类体内定义成员函数
#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. 使用NPOI实现简单的Excel导出功能

    [1]NPOI是啥? NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作. POI是一个开源的Java读写Excel. ...

  2. 5.Project常用操作介绍

    Project常用操作介绍 1.项目浏览器 2.项目属性 Name:项目名称 Category:项目组织结构 Author:作者 Copyright:版权 Image:项目图标 Description ...

  3. hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配

    此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...

  4. Firebird Character Sets and Collations

    Firebird Character Sets and Collations Every CHAR or VARCHAR field can (or, better: must) have a cha ...

  5. 获取 Windows Phone 的 User-Agent 字符串

    这个是简单介绍一下如何在wp开发中获取 Windows Phone 的 User-Agent 字符串,更多wp移动开发就去那个wp教程网吧.http://wp.662p.com 使用方法 public ...

  6. MFC多标签页对话框

    原文链接(有修改):http://blog.sina.com.cn/s/blog_6a1cdb3f0101llcw.html 1.新建一个MFC工程 取名PageSheet,选择Dialog base ...

  7. C# HttpWebRequest post 请求传参数

    Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表 paramet ...

  8. javaee字节流文件复制

    package Zy; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.Fil ...

  9. CF div2 499 A. Stages

    Code: #include<cstdio> #include<algorithm> #include<iostream> using namespace std; ...

  10. 【数据分析学习】Pandas学习记录

    import pandas as pd path = r'F:\数据分析专用\数据分析与机器学习\food_info.csv' with open(path, 'r') as f: data = pd ...