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 0;

}

2:

2.3 改写2.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 0;

}

c++面向对象程序设计 谭浩强

3:

#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 0;

}

4:

//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 0;

}

//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;

}

5:

//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 0;

}

//xt2-5-2.cpp(arraymax.cpp)

#include <iostream>

using namespace std;

#include "xt2-5.h"

void Array_max::set_value()

{ int i;

for (i=0;i<10;i++)

cin>>array[i];

}

void Array_max::max_value()

{int i;

max=array[0];

for (i=1;i<10;i++)

if(array[i]>max) max=array[i];

}

void Array_max::show_value()

{cout<<"max="<<max<<endl;

}

6:解法一

#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 0;

}

解法二:

#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 0;

}

原文地址:https://www.cnblogs.com/suibian1/p/10987274.html

时间: 2024-08-29 05:01:27

c++面向对象程序设计 谭浩强 第二章答案的相关文章

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

c++面向对象程序设计 谭浩强 答案 第一章 第1章  C++?的初步知识 1.请根据你的了解,叙述C++?的特点.C++?对C有哪些发展? [解] 略. 2.一个C++的程序是由哪几部分构成的?其中的每一部分起什么作用? [解] 略. 3.从拿到一个任务到得到最终结果,一般要经过几个步骤? [解] 略. 4.请说明编辑.编译.连接的作用.在编译后得到的目标文件为什么不能直接运行? [解] 编译是以源程序文件为单位进行的,而一个完整的程序可能包含若干个程序文件,在分别对它们编译之后,得到若干个目

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

2: #include <iostream> using namespace std; class Date {public: Date(int,int,int); Date(int,int); Date(int); Date(); void display(); private: int month; int day; int year; }; Date::Date(int m,int d,int y):month(m),day(d),year(y) { } Date::Date(int m

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

1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>>num>>name>>sex;} void display( ) {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<end

【c语言】c程序设计--谭浩强--(第一章)

闲来无事把谭浩强的书里边的题敲了一遍,,纪念一哈,,纯手工 // 在屏幕上输出 This is a c program. #include <stdio.h> int main() { printf("This is a c program.\n"); return 0; } // 求两个整数之和 #include <stdio.h> int main() { int a,b,c; printf("请输入两个整数:\n"); scanf(&q

【c语言】c程序设计--谭浩强--(第二章)--2

// 有两个瓶子a和b,分别盛放醋和酱油,要求将它们互换 #include <stdio.h> int main() { int a,b,c; printf("请输入两个整数:"); scanf("%d%d",&a,&b); c = a; a = b; b = c; printf("交换后的结果是:%d %d\n",a,b); return 0; } // 依次将10个数输入,要求输出其中最大的数 #include &

【c语言】c程序设计--谭浩强--(第三章)--2

// 假如我国国民生产总值的年增长率为9%,计算10年后我国国民生产总值与现在相比增长多少百分比 // p = ( 1 + r )^n r--年增长率 n--年数 p--与现在相比的倍数 #include <stdio.h> #include <math.h> int main() { double r = 0.09; double p; int n = 10; p = pow( ( 1 + r ) , n ); printf("10年后增长了 %f 倍\n",

【c语言】c程序设计--谭浩强--(第四章)--1

// 输入两个实数,按代数值由小到大的顺序输出这两个数 #include <stdio.h> int main() { int a,b; printf("请输入两个数:"); scanf("%d%d",&a,&b); if(a > b) printf("由小到大:%d %d\n",b,a); else printf("由小到大:%d %d\n",a,b); return 0; } // 输入3

【c语言】c程序设计--谭浩强--(第四章)--2

// 从键盘输入一个小于1000的正数,输出它的平方根(若平方根不是整数,则输出它的整数部分) // 要求在输入数据后检查是否为小于1000的正数,若不是则要求重新输入 #include <stdio.h> #include <math.h> int main() { int a; double b; printf("请输入一个小于1000的正数:"); scanf("%d",&a); if( a > 0 &&

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

c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private: double real; double imag;