《《C++面向对象程序设计答案》-第五章--谭浩强-.doc》由会员分享,可在线阅读,更多相关《《C++面向对象程序设计答案》-第五章--谭浩强-.doc(13页珍藏版)》请在金锄头文库上搜索。
1、1:#include using namespace std;class Studentpublic: void get_value() cinnumnamesex; void display( ) coutnum: numendl; coutname: nameendl; coutsex: sexageaddr; void display_1() coutage: ageendl; /引用派生类的私有成员,正确。 coutaddress: addrendl; /引用派生类的私有成员,正确。 private: int age; char addr30; ; int main() Student
2、1 stud1; stud1.get_value_1(); stud1.display(); stud1.display_1(); return 0;2:#include using namespace std;class Studentpublic: void get_value() cinnumnamesex; void display( ) coutnum: numendl; coutname: nameendl; coutsex: sexageaddr; void display_1() display(); coutage: ageendl; /引用派生类的私有成员,正确。 cout
3、address: addrendl; /引用派生类的私有成员,正确。 private: int age; char addr30; ; 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 name10; char sex;void
4、 Student:get_value() cinnumnamesex;void Student:display( ) coutnum: numendl; coutname: nameendl; coutsex: sexageaddr; void Student1:display1( ) coutnum: numendl; /引用基类的保护成员 coutname: nameendl; /引用基类的保护成员 coutsex: sexendl; /引用基类的保护成员 coutage: ageendl; /引用派生类的私有成员 coutaddress: addrendl; /引用派生类的私有成员 in
5、t main( ) Student1 stud1; /stud1是派生类student1类的对象 stud1.get_value_1(); /调用派生类对象stud1的公用成员函数 stud1.display1( ); /调用派生类对象stud1的公用成员函数 return 0; 4: 解法一#include using namespace std;class Student /声明基类public: /基类公用成员 void get_value(); void display( ); protected : /基类保护成员 int num; char name10; char sex;voi
6、d Student:get_value() cinnumnamesex;void Student:display( ) coutnum: numendl; coutname: nameendl; coutsex: sexageaddr; void Student1:display1( ) coutnum: numendl; /引用基类的保护成员,合法 coutname: nameendl; /引用基类的保护成员,合法 coutsex: sexendl; /引用基类的保护成员,合法 coutage: ageendl; /引用派生类的私有成员,合法 coutaddress: addrendl; /
7、引用派生类的私有成员,合法 int main( ) Student1 stud1; /stud1是派生类student1类的对象 stud1.get_value_1(); /调用派生类对象stud1的公用成员函数get_value_1 stud1.display1( ); /调用派生类对象stud1的公用成员函数display1 return 0; 解法二#include using namespace std;class Student /声明基类public: /基类公用成员 void get_value(); void display( ); protected : /基类保护成员 in
8、t num; char name10; char sex;void Student:get_value() cinnumnamesex;void Student:display( ) coutnum: numendl; coutname:nameendl; coutsex:sexageaddr; void Student1:display1( ) coutage:ageendl; coutaddress:addrendl; int main( ) Student1 stud1; /stud1是派生类student1类的对象 stud1.get_value(); stud1.get_value_1(); stud1.display( ); stud1.display1(); /合法。display1是派生类中的公用成员函数 retur