13周项目一.1(理解基类中成员的访问限定符和派生类的继承方式)
2013-05-29 10:54
639 查看
/* *程序的版权和版本声明部分 * Copyright (c)2013, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称:StudentA .cpp * 作 者: 田凤 *完成日期:2013年5月29日 * 版本号: v1.0 * 输入描述: 略 * 问题描述:略 * 输出:输出学生信息 *代码: #include <iostream> #include<string> using namespace std; class StudentA //(1)修改studentA类中各数据成员和成员函数的访问限定符,并观察发生的现象 { public: StudentA(int n,string nam,char s); void showA(); ~StudentA( ) { } protected: int num; string name; char sex ; }; class StudentB: public StudentA //(2)修改此处的继承方式,并观察发生的现象 { 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( ) //(3)思考由于访问限定符和继承方式的改变,会引发什么现象的发生,并作出解释 { 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】理解基类中成员的访问限定符和派生类的继承方式
- 【项目1 - 长颈鹿类对动物类的继承】理解基类中成员的访问限定符和派生类的继承方式
- 第十三周 项目一:理解基类中成员的访问限定符和派生类的继承方式
- 第十三周项目一—理解基类中成员的访问限定符和派生类的继承方式
- 第13周项目一-理解基类中成员的访问限定符和派生类的继承方式
- 第十二周C++【任务1】理解基类中成员的访问限定符和派生类的继承方式
- 第十二周实验指导--任务1--理解基类中成员的访问限定符和派生类的继承方式
- 第12周任务1(理解基类中成员的访问限定符和派生类的继承方式)
- 5月8日实验报告(一)理解基类成员的访问限定符和派生类的继承方式
- 继承项目第13周-项目1-基类中成员的访问限定符和派生类的继承方式
- 十二周实验报告任务1 理解基类中成员的访问限定符和派生类的继承方式。
- 第十二周任务一理解基类中成员的访问限定符和派生类的继承方式
- 理解基类中成员的访问限定符和派生类的继承方式
- 第十二周任务一 理解基类中成员的访问限定符和派生类的继承方式
- 第十二周任务二(理解基类成员的访问限定符和派生类的继承方式)
- C++第十二周【任务1】理解基类中成员的访问限定符和派生类的继承方式
- 第13周-项目1-基类中成员的访问限定符和派生类的继承方式
- 理解基类中成员的访问限定符和派生类的继承方式