第13周项目一-理解基类中成员的访问限定符和派生类的继承方式
2013-05-29 20:59
666 查看
/* * 程序的版权和版本声明部分 * Copyright (c)2012, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称:test.cpp * 作 者:徐凯旋 * 完成日期:2013 年 5 月 28 日 * 版 本 号: v1.0 */ #include <iostream> #include <string> using namespace std; class StudentA //(1)修改studentA类中各数据成员和成员函数的访问限定符为public时没有出现错误,修改为private时显示错误为error2248即私有成员不能再派生类内使用。 { public: StudentA(int n,string nam,char s); void showA(); ~StudentA( ) { } protected: int num; string name; char sex ; }; class StudentB: public StudentA //(2)将此处的继承方式改为protected时,出现错误。即保护成员只能在类内使用不能再类外使用。 { public: StudentB(int n,string nam,char s,int a,string ad); void showB( ); ~StudentB( ) { } private: int age; string addr; }; StudentA::StudentA(int n,string nam,char s) { num=n; name=nam; sex=s; } void StudentA::showA() { cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl<<endl; } StudentB::StudentB(int n,string nam,char s,int a,string ad):StudentA(n,nam,s) { age=a; addr=ad; } void StudentB::showB( ) { cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl<<endl; } int main( ) { StudentB stud1(10010,"Wang-li",'f',19,"115 Beijing Road,Shanghai"); StudentB stud2(10011,"Zhang-fun",'m',21,"213 Shanghai Road,Beijing"); StudentA stud3(20010,"He-xin",'m'); stud1.showB( ); stud2.showA( ); stud3.showA( ); return 0; }
运行结果:
相关文章推荐
- C++第十三周任务-项目一(理解基类中成员的访问限定符和派生类的继承方式)
- 第十三周—【项目1】理解基类中成员的访问限定符和派生类的继承方式
- 第十三周上机任务项目1-理解基类中成员的访问限定符和派生类的继承方式
- 第13周-项目1-基类中成员的访问限定符和派生类的继承方式
- 第十三周 项目一:理解基类中成员的访问限定符和派生类的继承方式
- 【项目1 - 长颈鹿类对动物类的继承】理解基类中成员的访问限定符和派生类的继承方式
- 第十三周项目一—理解基类中成员的访问限定符和派生类的继承方式
- 13周项目一.1(理解基类中成员的访问限定符和派生类的继承方式)
- 继承项目第13周-项目1-基类中成员的访问限定符和派生类的继承方式
- 第十二周任务二(理解基类成员的访问限定符和派生类的继承方式)
- C++第十二周【任务1】理解基类中成员的访问限定符和派生类的继承方式
- 理解基类中成员的访问限定符和派生类的继承方式
- 《C++第十二周实验报告1-1》----理解基类中成员的访问限定符和派生类的继承方式
- 第十二周C++【任务1】理解基类中成员的访问限定符和派生类的继承方式
- 第十二周实验指导--任务1--理解基类中成员的访问限定符和派生类的继承方式
- 第12周任务1(理解基类中成员的访问限定符和派生类的继承方式)
- 13_1理解基类中成员的访问限定符和派生类的继承方式
- 5月8日实验报告(一)理解基类成员的访问限定符和派生类的继承方式
- 十二周实验报告任务1 理解基类中成员的访问限定符和派生类的继承方式。
- 第十二周任务一理解基类中成员的访问限定符和派生类的继承方式