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的主要变化方面来看 ...
随机推荐
- Devexpress PdfViewer预览pdf,禁止下载,打印,复制
PDFviewer控件: 参数设置: 1.屏蔽书签栏和右键菜单 2.加载文档支持路径以及流stream加载的方式 pdfViewer.MenuManager.DisposeManager(); pdf ...
- FlappyBird模拟(不完整版本)
FlappyBird模拟(不完整版本) 准备材料 land地 sky天 pipe管道 bird小鸟 Land.js function Land(info) { this.x = info.x; thi ...
- Java基础6一面向对象
面向对象的编程思想:是以事物的整体的为基本单位,从事物的属性和行为两个方面进行描述. 特点: Java来源于生活服务于生活 用面向对象的思想能够接近正常的思维方式. 面向对象语言中有设计模式一说. 在 ...
- Android Studio ( Linux) 创建模拟器报错
Linux下Android studio创建模拟器最后一步报错 报错:An error occurred while creating the AVD. See idea.log for detail ...
- 读《我是一只 IT 小小鸟》
读<我是一只 IT 小小鸟> 作为一个一向看重节操的体面人,即使面临许多 DDL 包括期中考试,在忙乱不堪的时候我也断不断告诫自己,不能迫于课程要求仅为了写出一篇笔记而去读书,以后更是如此 ...
- MySQL 5.6 Reference Manual-14.6 InnoDB Table Management
14.6 InnoDB Table Management 14.6.1 Creating InnoDB Tables 14.6.2 Moving or Copying InnoDB Tables to ...
- 开源作品-PHP写的在线文件管理工具(单文件绿色版)-SuExplorer_PHP_3_0
前言:项目开发过程中,网站一般部署到远程服务器,所以文件管理就不能和本机操作一样方便.通常文件管理是用ftp下载到本地,修改后再上传,或者远程登录到服务器进行修改.但是这些操作都依赖于复杂的第三方软件 ...
- Fear No More歌词
"Fear No More" Every anxious thought that steals my breath It's a heavy weight upon my ...
- vc++如何创建程序-函数的重载
重载构成的条件:函数的参数类型,参数个数不同,才能构成函数的重载 函数重载分为两种情况: 1 .(1)void output(); (2)int output(); 2 .(1)void output ...
- CentOS6.6安装mysql-5.7.25二进制安装包简易教程
#####1,安装前首先确认系统版本 [root@bogon:~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@bogon:~] ...