答案-c++面向对象程序设计课后习题答案(谭浩强版)

box1.volume();

cout<<\ box1.display(); box2.get_value(); box2.volume();

cout<<\ box2.display(); box3.get_value(); box3.volume();

cout<<\ box3.display(); return 0; }

第三章

2:

#include 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,int d):month(m),day(d) {year=2005;}

Date::Date(int m):month(m) {day=1;

year=2005; }

Date::Date() {month=1; day=1; year=2005; }

void Date::display()

{cout<

int main() {

Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; } 3:

#include using namespace std; class Date {public:

Date(int=1,int=1,int=2005); void display(); private: int month; int day; int year; };

Date::Date(int m,int d,int y):month(m),day(d),year(y) { }

void Date::display()

{cout<

int main() {

Date d1(10,13,2005); Date d2(12,30);

Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; } 4:

#include using namespace std; class Student {public:

Student(int n,float s):num(n),score(s){} void display(); private: int num; float score; };

void Student::display()

{cout<

int main()

{Student stud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; Student *p=stud;

for(int i=0;i<=2;p=p+2,i++) p->display(); return 0; } 5:

#include using namespace std; class Student {public:

Student(int n,float s):num(n),score(s){} int num; float score; };

void main()

{Student stud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; void max(Student* ); Student *p=&stud[0]; max(p); }

void max(Student *arr)

{float max_score=arr[0].score; int k=0;

for(int i=1;i<5;i++)

if(arr[i].score>max_score) {max_score=arr[i].score;k=i;} cout<

#include using namespace std; class Student {public:

Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display(){cout<

int main()

{Student stud(101,78.5); stud.display();

stud.change(101,80.5); stud.display(); return 0; }

7: 解法一

#include using namespace std; class Student {public:

Student(int n,float s):num(n),score(s){}

联系客服:779662525#qq.com(#替换为@)