类体内定义成员函数
#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++面向对象程序设计 谭浩强 第二章答案的更多相关文章

  1. c++面向对象程序设计 谭浩强 第一章答案

    c++面向对象程序设计 谭浩强 答案 第一章 目录: c++面向对象程序设计 谭浩强 答案 第一章 c++面向对象程序设计 谭浩强 答案 第二章 c++面向对象程序设计 谭浩强 答案 第三章 c++面 ...

  2. c++面向对象程序设计 谭浩强 第三章答案

    2: #include <iostream> using namespace std; class Date {public: Date(int,int,int); Date(int,in ...

  3. c++面向对象程序设计 谭浩强 第五章答案

    1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>&g ...

  4. C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂

    C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...

  5. 关于指针的笔记【1】【C语言程序设计-谭浩强】

    指针是什么? 一个 变量的地址称为该变量的"指针"[将地址形象化的称为“指针”].(指针是什么百度百科) 注意区分储存单元的地址和内容这两个概念的区别. 直接访问:直接按变量名进行 ...

  6. c++面向对象程序设计 课后题 答案 谭浩强 第四章

    c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...

  7. C程序设计(谭浩强)第五版课后题答案 第一章

    大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...

  8. 再论谭浩强《C语言程序设计》

    一些同学学不好C语言,把罪责归于“因为教材是谭浩强写的”实在是很滑稽. 谭浩强老先生 1934 年生,现在已经 80 岁了.他 1958 年从清华大学自动控制系毕业,那年 24 岁.要知道 C 语言那 ...

  9. 挂羊头卖狗肉蓄意欺骗读者——谭浩强《C程序设计(第四版)》中所谓的“按照C99”(二)

    挂羊头卖狗肉蓄意欺骗读者——谭浩强<C程序设计(第四版)>中所谓的“按照C99”(二) 在<谭C>p4:“本书的叙述以C99标准为依据”,下面从C89到C99的主要变化方面来看 ...

随机推荐

  1. NFS 开机自动挂载共享目录

    开机自动挂载: 如果服务端或客户端的服务器重启之后需要手动挂载,我们可以加入到开机自动挂载 在服务端/客户端的/etc/fstab里添加 192.168.22.204:/opt/filestore  ...

  2. lua lfs库

     lfs.attributes(filepath [, aname]) 获取路径指定属性    lfs.chdir(path) 改变当前工作目录,成功返回true,失败返回nil加上错误信息    l ...

  3. 错误:Camera录制视频(6.0错误),5.1正常,7.1正常 (java.lang.RuntimeException: start failed.at android.media.MediaRecorder.native_start(Native Method))

    Process: com.example.mycamera2, PID: 24086 java.lang.RuntimeException: start failed. at android.medi ...

  4. IIS网站调用FFMPEG执行截图命令失败的原因(暂时记录)

    之前有个功能是用户上传视频后,网站对其进行截图处理.功能采用FFMPEG操作,在本机新建一个小程序进行调试时是没有问题的. 可功能原封不动挪到项目里,截图竟然失败了,于是开始排查. 期初以为是参数设置 ...

  5. Wireshark抓本地回环

    最近正好要分析下本机两个端口之间通信状况.于是用wireshark抓包分析.对于本地回环要进行一些特殊的设置. 1.通过“运行”---“cmd” 输入“route add [本机IP]mask 255 ...

  6. js 关于时间

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  7. 转载:html特殊字符 编码css3 content:&amp;quot;我是特殊符号&amp;quot;

    项目中用到的一些特殊字符和图标 html代码 <div class="cross"></div> css代码 .cross{ width: 20px; he ...

  8. day25-1 time,datetime模块

    目录 time 为什么要有time模块,time模块有什么用 时间戳形式 格式化时间 结构化时间 各种时间格式互相转换 datetime 为什么要有datetime模块,detatime模块有什么用 ...

  9. 路飞学城Python-Day142

    第2节:UA身份伪装 反爬机制 User-Agent:请求载体的身份标识 通过不同的手段的当前的请求载体是不一样的,请求信息也是不一样的,常见的请求信息都是以键和值的形式存在 浏览器的开发者工具 Ne ...

  10. rm -fr删除不了文件

    向各位求教:一个阿里的 ecs服务器,放网站的.估计被挂马了,其中网站下的一个文件index.html,被篡改,想删除,但是删除不了. ls -l 结果:-r--r--r--  1 www  www  ...