c++面向对象程序设计 谭浩强 第二章答案
类体内定义成员函数
#include <iostream> using namespace std;
class Time { public: void set_time();
void show_time();
private: //成员改为公用的 int hour;
int minute;
int sec;
};
void Time::set_time() //在main函数之前定义 { cin >> hour;
cin >> minute;
cin >> sec;
} void Time::show_time() //在main函数之前定义 { cout << hour << ":" << minute << ":" << sec << endl;
} int main() {
Time t1;
t1.set_time();
t1.show_time();
return ;
} : 2.3 改写2.:类体内声明成员函数,类外定义成员函数
#include <iostream> using namespace std;
class Time {
public: void set_time(void) {
cin >> hour;
cin >> minute;
cin >> sec;
} void show_time(void) {
cout << hour << ":" << minute << ":" << sec << endl;
} private: int hour;
int minute;
int sec;
};
c++面向对象程序设计 答案 Time t;
int main() { t.set_time();
t.show_time();
return ;
} c++面向对象程序设计 谭浩强 : #include <iostream> using namespace std;
class Time {
public: void set_time(void);
void show_time(void);
private: int hour;
int minute;
int sec;
};
c++面向对象程序设计 谭浩强 void Time::set_time(void) {
cin >> hour;
cin >> minute;
cin >> sec;
} void Time::show_time(void)
{
cout << hour << ":" << minute << ":" << sec << endl;
} Time t;
int main() {
t.set_time();
t.show_time();
return ;
} : //xt2-4-1.cpp(main.cpp) #include <iostream> using namespace std;
#include "xt2-4.h" int main() {
Student stud;
stud.set_value();
stud.display();
return ;
} //xt2-4-2.cpp(即student.cpp) #include "xt2-4.h" //在此文件中进行函数的定义 #include <iostream> using namespace std; //不要漏写此行 void Student::display() {
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
} void Student::set_value() {
cin >> num;
cin >> name;
cin >> sex;
} : //xt2-5-1.cpp(file1.cpp) #include <iostream> #include "xt2-5.h" int main() {
Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return ;
} //xt2-5-2.cpp(arraymax.cpp) #include <iostream> using namespace std;
#include "xt2-5.h" void Array_max::set_value() {
int i;
for (i = ; i < ; i++) cin >> array[i];
} void Array_max::max_value() {
int i;
max = array[];
for (i = ; i<; i++) if (array[i]>max)
max = array[i];
} void Array_max::show_value() {
cout << "max=" << max << endl;
} :解法一 #include <iostream> using namespace std;
class Box {
public: void get_value();
float volume();
void display();
public: float lengh;
float width;
float height;
}; void Box::get_value() {
cout << "please input lengh, width,height:";
cin >> lengh;
cin >> width;
cin >> height;
} float Box::volume() {
return(lengh*width*height);
} void Box::display() {
cout << volume() << endl;
} int main() {
Box box1, box2, box3;
box1.get_value();
cout << "volmue of bax1 is ";
box1.display();
box2.get_value();
cout << "volmue of bax2 is ";
box2.display();
box3.get_value();
cout << "volmue of bax3 is ";
box3.display();
return ;
} 解法二: #include <iostream> using namespace std;
class Box {
public: void get_value();
void volume();
void display();
public: float lengh;
float width;
float height;
float vol;
}; void Box::get_value() {
cout << "please input lengh, width,height:";
cin >> lengh;
cin >> width;
cin >> height;
} void Box::volume() {
vol = lengh*width*height;
} void Box::display() {
cout << vol << endl;
} int main() {
Box box1, box2, box3;
box1.get_value();
box1.volume();
cout << "volmue of bax1 is ";
box1.display();
box2.get_value();
box2.volume();
cout << "volmue of bax2 is ";
box2.display();
box3.get_value();
box3.volume();
cout << "volmue of bax3 is ";
box3.display();
return ;
}
c++面向对象程序设计 谭浩强 第二章答案的更多相关文章
- c++面向对象程序设计 谭浩强 第一章答案
c++面向对象程序设计 谭浩强 答案 第一章 目录: c++面向对象程序设计 谭浩强 答案 第一章 c++面向对象程序设计 谭浩强 答案 第二章 c++面向对象程序设计 谭浩强 答案 第三章 c++面 ...
- c++面向对象程序设计 谭浩强 第三章答案
2: #include <iostream> using namespace std; class Date {public: Date(int,int,int); Date(int,in ...
- c++面向对象程序设计 谭浩强 第五章答案
1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>&g ...
- C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂
C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...
- 关于指针的笔记【1】【C语言程序设计-谭浩强】
指针是什么? 一个 变量的地址称为该变量的"指针"[将地址形象化的称为“指针”].(指针是什么百度百科) 注意区分储存单元的地址和内容这两个概念的区别. 直接访问:直接按变量名进行 ...
- c++面向对象程序设计 课后题 答案 谭浩强 第四章
c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...
- C程序设计(谭浩强)第五版课后题答案 第一章
大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...
- 再论谭浩强《C语言程序设计》
一些同学学不好C语言,把罪责归于“因为教材是谭浩强写的”实在是很滑稽. 谭浩强老先生 1934 年生,现在已经 80 岁了.他 1958 年从清华大学自动控制系毕业,那年 24 岁.要知道 C 语言那 ...
- 挂羊头卖狗肉蓄意欺骗读者——谭浩强《C程序设计(第四版)》中所谓的“按照C99”(二)
挂羊头卖狗肉蓄意欺骗读者——谭浩强<C程序设计(第四版)>中所谓的“按照C99”(二) 在<谭C>p4:“本书的叙述以C99标准为依据”,下面从C89到C99的主要变化方面来看 ...
随机推荐
- Rsync同步神器
Rsync清理大批量垃圾数据 在Linux下删除海量文件的情况,需要删除数十万个文件.这个是之前的程序写的日志,增长很快,而且没什么用.这个时候,我们常用的删除命令rm -fr * 就不好用了,因为要 ...
- Unity3d 销毁
using UnityEngine; using System.Collections; public class destroy : MonoBehaviour { void Start () { ...
- Java基础10一面向对象
抽象 概念:当一个类中没有足够的信息描述一个现实生活中具体存在的事物,那么这个类就是抽象类. 抽象类一般是对概念领域中的描述. 语法: [访问修饰符] abstract class 类名{ } 如: ...
- javascript中模块化知识总结
JavaScript 模块化开发 1. 模块化介绍 掌握模块化基本概念以及使用模块化带来的好处 当你的网站开发越来越复杂的时候,会经常遇到什么问题? 恼人的命名冲突 繁琐的文件依赖 历史上,JavaS ...
- 00--Linux常用命令大全
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- 在maven项目结构下对于Resources目录下文件的存取
在maven项目中,文件结构如下: proj ---src ----main ----java ----Main.java ----resources ----userFile.properties ...
- 从操作系统内核看设计模式--linux内核的facade模式
linux的内核当中处处充满了设计模式,本文先讨论一下外观模式.外观模式就是将客户和子系统解耦,为客户将复杂的子系统进行封装,从而使得客户可以使用简单易用的接口. 众所周知,linux和unix是十 ...
- PhotoZoom放大图片,真的能无损吗?
当然想要无损放大一张很小的图片时,总会有人会和你推荐PhotoZoom这款软件,那么它真的和说的一样,可以无损放大吗?下面小编挑了2张图片做了一下对比. 案例1,我们选取一张尺寸为200x200像素的 ...
- Dynamics CRM Online 快速的debug 方法
这里的前提想大家了解一下. Dynamics 365 online的产品的session是30分钟 timeout. 如果你logout之后, session还是会储存在服务器端不会release. ...
- Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档
0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...