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

cout<<"sex: "<<sex<<endl;}

private :

int num;

char name[10];

char sex;

};

class Student1: public Student

{public:

void get_value_1()

{get_value();

cin>>age>>addr;}

void display_1()

{   cout<<"age: "<<age<<endl;          //引用派生类的私有成员,正确。

cout<<"address: "<<addr<<endl;}    //引用派生类的私有成员,正确。

private:

int age;

char addr[30];

};

int  main()

{Student1 stud1;

stud1.get_value_1();

stud1.display();

stud1.display_1();

return 0;

}

2:

#include <iostream>

using namespace std;

class Student

{public:

void get_value()

{cin>>num>>name>>sex;}

void display( )

{cout<<"num: "<<num<<endl;

cout<<"name: "<<name<<endl;

cout<<"sex: "<<sex<<endl;}

private :

int num;

char name[10];

char sex;

};

class Student1: private Student

{public:

void get_value_1()

{get_value();

cin>>age>>addr;}

void display_1()

{display();

cout<<"age: "<<age<<endl;        //引用派生类的私有成员,正确。

cout<<"address: "<<addr<<endl;}    //引用派生类的私有成员,正确。

private:

int age;

char addr[30];

};

int main()

{Student1 stud1;

stud1.get_value_1();

stud1.display_1();

return 0;

}

3:

#include <iostream>

using namespace std;

class Student                        //声明基类

{public:                             //基类公用成员

void get_value();

void display( );

protected :                         //基类保护成员

int num;

char name[10];

char sex;

};

void Student::get_value()

{cin>>num>>name>>sex;}

void Student::display( )

{cout<<"num: "<<num<<endl;

cout<<"name: "<<name<<endl;

cout<<"sex: "<<sex<<endl;

}

class Student1: protected Student              //声明一个保护派生类

{public:

void get_value_1();

void display1( );

private:

int age;

char addr[30];

};

void Student1::get_value_1()

{get_value();

cin>>age>>addr;

}

void Student1::display1( )

{cout<<"num: "<<num<<endl;         //引用基类的保护成员

cout<<"name: "<<name<<endl;       //引用基类的保护成员

cout<<"sex: "<<sex<<endl;         //引用基类的保护成员

cout<<"age: "<<age<<endl;         //引用派生类的私有成员

cout<<"address: "<<addr<<endl;    //引用派生类的私有成员

}

int main( )

{Student1 stud1;                      //stud1是派生类student1类的对象

stud1.get_value_1();                 //调用派生类对象stud1的公用成员函数

stud1.display1( );                   //调用派生类对象stud1的公用成员函数

return 0;

}

4: 解法一

#include <iostream>

using namespace std;

class Student//声明基类

{public:                             //基类公用成员

void get_value();

void display( );

protected :                         //基类保护成员

int num;

char name[10];

char sex;

};

void Student::get_value()

{cin>>num>>name>>sex;}

void Student::display( )

{cout<<"num: "<<num<<endl;

cout<<"name: "<<name<<endl;

cout<<"sex: "<<sex<<endl;

}

class Student1: public Student              //声明一个公用派生类

{public:

void get_value_1();

void display1( );

private:

int age;

char addr[30];

};

void Student1::get_value_1()

{get_value();

cin>>age>>addr;

}

void Student1::display1( )

{cout<<"num: "<<num<<endl;     //引用基类的保护成员,合法

cout<<"name: "<<name<<endl;       //引用基类的保护成员,合法

cout<<"sex: "<<sex<<endl;         //引用基类的保护成员,合法

cout<<"age: "<<age<<endl;         //引用派生类的私有成员,合法

cout<<"address: "<<addr<<endl;    //引用派生类的私有成员,合法

}

int main( )

{Student1 stud1;                      //stud1是派生类student1类的对象

stud1.get_value_1();                 //调用派生类对象stud1的公用成员函数get_value_1

stud1.display1( );                   //调用派生类对象stud1的公用成员函数display1

return 0;

}

解法二

#include <iostream>

using namespace std;

class Student                        //声明基类

{public:                             //基类公用成员

void get_value();

void display( );

protected :                         //基类保护成员

int num;

char name[10];

char sex;

};

void Student::get_value()

{cin>>num>>name>>sex;}

void Student::display( )

{cout<<"num: "<<num<<endl;

cout<<"name:"<<name<<endl;

cout<<"sex:"<<sex<<endl;

}

class Student1: protected Student              //声明一个公用派生类

{public:

void get_value_1();

void display1( );

private:

int age;

char addr[30];

};

void Student1::get_value_1()

{cin>>age>>addr;}

void Student1::display1( )

{cout<<"age:"<<age<<endl;

cout<<"address:"<<addr<<endl;

}

int main( )

{Student1 stud1;                      //stud1是派生类student1类的对象

stud1.get_value();

stud1.get_value_1();

stud1.display( );

stud1.display1();                  //合法。display1是派生类中的公用成员函数

return 0;

}

5:

class A                   //A为基类

{public:

void f1( );

int i;

protected:

void f2();

int j;

private:

int k;

};

class B: public A            //B为A的公用派生类

{public:

void f3( );

protected:

int m;

private:

int n;

};

class C: public B              //C为B的公用派生类

{public:

void f4();

private:

int p;

};

int main()

{A a1;                         //a1是基类A的对象

B b1;                         //b1是派生类B的对象

C//c1是派生类C的对象

return 0;

}

6:

#include <iostream>

using namespace std;

class A

{public:

void f1( );

protected:

void f2();

private:

int i;

};

class B: public A

{public:

void f3( );

int k;

private:

int m;

};

class C: protected B

{public:

void f4();

protected:

int n;

private:

int p;

};

class D: private C

{public:

void f5();

protected:

int q;

private:

int r;

};

int main()

{A a1;

B b1;

C c1;

D d1;

return 0;

}

7:

#include <iostream>

using namespace std;

class A

{

public:

A(){a=0;b=0;}

A(int i){a=i;b=0;}

A(int i,int j){a=i;b=j;}

void display(){cout<<"a="<<a<<" b="<<b;}

private:

int a;

int b;

};

class B  : public A

{

public:

B(){c=0;}

B(int i):A(i){c=0;}

B(int i,int j):A(i,j){c=0;}

B(int i,int j,int k):A(i,j){c=k;}

void display1()

{display();

cout<<" c="<<c<<endl;

}

private:

int c;

};

int main()

{   B b1;

B b2(1);

B b3(1,3);

B b4(1,3,5);

b1.display1();

b2.display1();

b3.display1();

b4.display1();

return 0;

}

8:

#include <iostream>

using namespace std;

class A

{

public:

A(){cout<<"constructing A "<<endl;}

~A(){cout<<"destructing A "<<endl;}

};

class B  : public A

{

public:

B(){cout<<"constructing B "<<endl;}

~B(){cout<<"destructing B "<<endl;}

};

class C  : public B

{

public:

C(){cout<<"constructing C "<<endl;}

~C(){cout<<"destructing C "<<endl;}

};

int main()

{ C c1;

return 0;

}

9:

#include<string>

#include <iostream>

using namespace std;

class Teacher

{public:

Teacher(string nam,int a,char s,string tit,string ad,string t);

void display();

protected:

string name;

int age;

char sex;

string title;

string addr;

string tel;

};

Teacher::Teacher(string nam,int a,char s,string tit,string ad,string t):

name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){ }

void Teacher::display()

{cout<<"name:"<<name<<endl;

cout<<"age"<<age<<endl;

cout<<"sex:"<<sex<<endl;

cout<<"title:"<<title<<endl;

cout<<"address:"<<addr<<endl;

cout<<"tel:"<<tel<<endl;

}

class Cadre

{public:

Cadre(string nam,int a,char s,string p,string ad,string t);

void display();

protected:

string name;

int age;

char sex;

string post;

string addr;

string tel;

};

Cadre::Cadre(string nam,int a,char s,string p,string ad,string t):

name(nam),age(a),sex(s),post(p),addr(ad),tel(t){}

void Cadre::display()

{cout<<"name:"<<name<<endl;

cout<<"age:"<<age<<endl;

cout<<"sex:"<<sex<<endl;

cout<<"post:"<<post<<endl;

cout<<"address:"<<addr<<endl;

cout<<"tel:"<<tel<<endl;

}

class Teacher_Cadre:public Teacher,public Cadre

{public:

Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w);

void show( );

private:

float wage;

};

Teacher_Cadre::Teacher_Cadre(string nam,int a,char s,string t,string p,string ad,string tel,float w):

Teacher(nam,a,s,t,ad,tel),Cadre(nam,a,s,p,ad,tel),wage(w) {}

void Teacher_Cadre::show( )

{Teacher::display();

cout<<"post:"<<Cadre::post<<endl;

cout<<"wages:"<<wage<<endl;

}

int main( )

{Teacher_Cadre te_ca("Wang-li",50,‘f‘,"prof.","president","135 Beijing Road,Shanghai","(021)61234567",1534.5);

te_ca.show( );

return 0;

}

10:

#include <iostream>

#include <cstring>

using namespace std;

class Teacher                                //教师类

{public:

Teacher(int,char [],char);               //声明构造函数

void display();                          //声明输出函数

private:

int num;

char name[20];

char sex;

};

Teacher::Teacher(int n,char nam[],char s)    //定义构造函数

{num=n;

strcpy(name,nam);

sex=s;

}

void Teacher::display()                      //定义输出函数

{cout<<"num:"<<num<<endl;

cout<<"name:"<<name<<endl;

cout<<"sex:"<<sex<<endl;

}

class BirthDate                               //生日类

{public:

BirthDate(int,int,int);                   //声明构造函数

void display();                           //声明输出函数

void change(int,int,int);                 //声明修改函数

private:

int year;

int month;

int day;

};

BirthDate::BirthDate(int y,int m,int d)       //定义构造函数

{year=y;

month=m;

day=d;

}

void BirthDate::display()                     //定义输出函数

{cout<<"birthday:"<<month<<"/"<<day<<"/"<<year<<endl;}

void BirthDate::change(int y,int m,int d)     //定义修改函数

{year=y;

month=m;

day=d;

}

class Professor:public Teacher                         //教授类

{public:

Professor(int,char [],char,int,int,int,float);    //声明构造函数

void display();                                   //声明输出函数

void change(int,int,int);                         //声明修改函数

private:

float area;

BirthDate birthday;                               //定义BirthDate类的对象作为数据成员

};

Professor::Professor(int n,char nam[20],char s,int y,int m,int d,float a):

Teacher(n,nam,s),birthday(y,m,d),area(a){ }          //定义构造函数

void Professor::display()                             //定义输出函数

{Teacher::display();

birthday.display();

cout<<"area:"<<area<<endl;

}

void Professor::change(int y,int m,int d)             //定义修改函数

{birthday.change(y,m,d);

}

int main()

{Professor prof1(3012,"Zhang",‘f‘,1949,10,1,125.4);   //定义Professor对象prof1

cout<<endl<<"original data:"<<endl;

prof1.display();                                     //调用prof1对象的display函数

cout<<endl<<"new data:"<<endl;

prof1.change(1950,6,1);                              //调用prof1对象的change函数

prof1.display();                                     //调用prof1对象的display函数

return 0;

}

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

时间: 2024-07-31 12:25:46

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

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++面向对象程序设计 谭浩强 第一章答案

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

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

【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++面向对象程序设计课后题答案 谭浩强 第四章 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;

【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 &&