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

double imag; };

void Complex::display()

{cout<<\

int main()

{Complex c1(3,4),c2; double d1; d1=2.5+c1;

cout<<\ c2=Complex(d1); cout<<\ c2.display(); return 0; } 7:

#include using namespace std; class Student {public:

Student(int,char[],char,float); int get_num(){return num;}

char * get_name(){return name;} char get_sex(){return sex;} void display()

{cout<<\ private: int num;

char name[20]; char sex; float score; };

Student::Student(int n,char nam[],char s,float so) {num=n;

strcpy(name,nam); sex=s; score=so; }

class Teacher {public: Teacher(){}

Teacher(Student&);

Teacher(int n,char nam[],char sex,float pay); void display(); private: int num;

char name[20]; char sex; float pay; };

Teacher::Teacher(int n,char nam[],char s,float p) {num=n;

strcpy(name,nam); sex=s; pay=p; }

Teacher::Teacher(Student& stud) {num=stud.get_num();

strcpy(name,stud.get_name()); sex=stud.get_sex(); pay=1500;}

void Teacher::display()

{cout<<\

int main()

{Teacher teacher1(10001,\ Student student1(20010,\ cout<<\ student1.display();

teacher2=Teacher(student1); cout<<\ teacher2.display(); return 0; }

第五章

1:

#include using namespace std; class Student

{public:

void get_value()

{cin>>num>>name>>sex;} void display( )

{cout<<\ cout<<\ cout<<\ 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<<\ cout<<\ private:

int age;

char addr[30]; };

int main()

{Student1 stud1;

stud1.get_value_1(); stud1.display(); stud1.display_1(); return 0; } 2:

#include using namespace std; class Student {public:

void get_value()

{cin>>num>>name>>sex;} void display( )

{cout<<\ cout<<\ cout<<\

//引用派生类的私有成员,正确。//引用派生类的私有成员,正确。 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<<\ //引用派生类的私有成员,正确。 cout<<\ //引用派生类的私有成员,正确。 private:

int age; char addr[30]; };

int main()

{Student1 stud1;

stud1.get_value_1(); stud1.display_1(); return 0; } 3:

#include 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<<\

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