C++程序设计第三版(谭浩强)第十一章习题答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C++程序设计第三版(谭浩强)第十一章习题答案
11.1 题
#include
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<
的私有成员,正确。
cout<<"address: "<<addr<
的私有成员,正确。
private:
int age;
char addr[30];
};
int main()
{Student1 stud1;
stud1.get_value_1();
stud1.display();
stud1.display_1();
return 0;
}
11.2 题
#include
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<
私有成员,正确。
cout<<"address: "<<addr< 的私有成员,正确。
private:
int age;
char addr[30];
};
int main()
{Student1 stud1;
stud1.get_value_1();
stud1.display_1();
return 0;
}
11.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<<"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<
成员
cout<<"name: "<<name<
成员
cout<<"sex: "<<sex<
员
cout<<"age: "<<age<
有成员
cout<<"address: "<<addr<
有成员
}
int main( )
{Student1 stud1; //stud1 是派生类student1 类的对象
stud1.get_value_1(); //调用派生类对象stud1 的公用成员函数
stud1.display1( ); //调用派生类对象stud1 的公用成员函数
return 0;
}
11.4 题
#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<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;
}
class Student1: public Student //声明一
</sex<<endl;
</name<<endl;
</num<<endl;
</addr<</age<</sex<</name<</num<</sex<<endl; </name<<endl;
</num<<endl;
</addr<</age<</sex<<endl;}
</name<<endl;
</num<<endl;
</addr<</age<</sex<<endl;}
</name<<endl;
</num<<endl;。