《C++面向对象程序设计答案》第五章谭浩强清华大学出版社.docx
- 文档编号:25151556
- 上传时间:2023-06-05
- 格式:DOCX
- 页数:20
- 大小:16.51KB
《C++面向对象程序设计答案》第五章谭浩强清华大学出版社.docx
《《C++面向对象程序设计答案》第五章谭浩强清华大学出版社.docx》由会员分享,可在线阅读,更多相关《《C++面向对象程序设计答案》第五章谭浩强清华大学出版社.docx(20页珍藏版)》请在冰豆网上搜索。
《C++面向对象程序设计答案》第五章谭浩强清华大学出版社
1:
#include
usingnamespacestd;
classStudent
{public:
voidget_value()
{cin>>num>>name>>sex;}
voiddisplay()
{cout<<"num:
"< cout<<"name: "< cout<<"sex: "< private: intnum; charname[10]; charsex; }; classStudent1: publicStudent {public: voidget_value_1() {get_value(); cin>>age>>addr;} voiddisplay_1() {cout<<"age: "< cout<<"address: "< private: intage; charaddr[30]; }; intmain() {Student1stud1; stud1.get_value_1(); stud1.display(); stud1.display_1(); return0; } 2: #include usingnamespacestd; classStudent {public: voidget_value() {cin>>num>>name>>sex;} voiddisplay() {cout<<"num: "< cout<<"name: "< cout<<"sex: "< private: intnum; charname[10]; charsex; }; classStudent1: privateStudent {public: voidget_value_1() {get_value(); cin>>age>>addr;} voiddisplay_1() {display(); cout<<"age: "< cout<<"address: "< private: intage; charaddr[30]; }; intmain() {Student1stud1; stud1.get_value_1(); stud1.display_1(); return0; } 3: #include usingnamespacestd; classStudent//声明基类 {public: //基类公用成员 voidget_value(); voiddisplay(); protected: //基类保护成员 intnum; charname[10]; charsex; }; voidStudent: : get_value() {cin>>num>>name>>sex;} voidStudent: : display() {cout<<"num: "< cout<<"name: "< cout<<"sex: "< } classStudent1: protectedStudent//声明一个保护派生类 {public: voidget_value_1(); voiddisplay1(); private: intage; charaddr[30]; }; voidStudent1: : get_value_1() {get_value(); cin>>age>>addr; } voidStudent1: : display1() {cout<<"num: "< cout<<"name: "< cout<<"sex: "< cout<<"age: "< cout<<"address: "< } intmain() {Student1stud1;//stud1是派生类student1类的对象 stud1.get_value_1();//调用派生类对象stud1的公用成员函数 stud1.display1();//调用派生类对象stud1的公用成员函数 return0; } 4: 解法一 #include usingnamespacestd; classStudent//声明基类 {public: //基类公用成员 voidget_value(); voiddisplay(); protected: //基类保护成员 intnum; charname[10]; charsex; }; voidStudent: : get_value() {cin>>num>>name>>sex;} voidStudent: : display() {cout<<"num: "< cout<<"name: "< cout<<"sex: "< } classStudent1: publicStudent//声明一个公用派生类 {public: voidget_value_1(); voiddisplay1(); private: intage; charaddr[30]; }; voidStudent1: : get_value_1() {get_value(); cin>>age>>addr; } voidStudent1: : display1() {cout<<"num: "< cout<<"name: "< cout<<"sex: "< cout<<"age: "< cout<<"address: "< } intmain() {Student1stud1;//stud1是派生类student1类的对象 stud1.get_value_1();//调用派生类对象stud1的公用成员函数get_value_1 stud1.display1();//调用派生类对象stud1的公用成员函数display1 return0; } 解法二 #include usingnamespacestd; classStudent//声明基类 {public: //基类公用成员 voidget_value(); voiddisplay(); protected: //基类保护成员 intnum; charname[10]; charsex; }; voidStudent: : get_value() {cin>>num>>name>>sex;} voidStudent: : display() {cout<<"num: "< cout<<"name: "< cout<<"sex: "< } classStudent1: protectedStudent//声明一个公用派生类 {public: voidget_value_1(); voiddisplay1(); private: intage; charaddr[30]; }; voidStudent1: : get_value_1() {cin>>age>>addr;} voidStudent1: : display1() {cout<<"age: "< cout<<"address: "< } intmain() {Student1stud1;//stud1是派生类student1类的对象 stud1.get_value(); stud1.get_value_1(); stud1.display(); stud1.display1();//合法。 display1是派生类中的公用成员函数 return0; } 5: classA//A为基类 {public: voidf1(); inti; protected: voidf2(); intj; private: intk; }; classB: publicA//B为A的公用派生类 {public: voidf3(); protected: intm; private: intn; }; classC: publicB//C为B的公用派生类 {public: voidf4(); private: intp; }; intmain() {Aa1;//a1是基类A的对象 Bb1;//b1是派生类B的对象 Cc1;//c1是派生类C的对象 return0; } 6: #include usingnamespacestd; classA {public: voidf1(); protected: voidf2(); private: inti; }; classB: publicA {public: voidf3(); intk; private: intm; }; classC: protectedB {public: voidf4(); protected: intn; private: intp; }; classD: privateC {public: voidf5(); protected: intq; private: intr; }; intmain() {Aa1; Bb1; Cc1; Dd1; return0; } 7: #include usingnamespacestd; classA { public: A(){a=0;b=0;} A(inti){a=i;b=0;} A(inti,intj){a=i;b=j;}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+面向对象程序设计答案 C+ 面向 对象 程序设计 答案 第五 章谭浩强 清华大学出版社
![提示](https://static.bdocx.com/images/bang_tan.gif)