派生,继承1
来源:互联网 发布:mac地址定位上网位置 编辑:程序博客网 时间:2024/06/07 03:01
/** 程序的版权和版本声明部分* Copyright (c)2012, 烟台大学计算机学院学生* All rightsreserved.* 文件名称: x.cpp* 作者:徐本锡* 完成日期: 2013年5 月 28 日* 版本号: v1.0* 输入描述:* 问题描述:理解基类中成员的访问限定符和派生类的继承方式由下面派生类StudentB对基类StudentA的继承……* 程序输出:*///我的代码:#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;}//(1)尝试改变并运行// 请修改基类中成员的访问限定符和派生类的继承方式,考虑可能的运行结果或可能出现的错误,并在上机时进行验证、对比,达到理解派生类成员的访问属性的目的。//(2)总结// 记录(1)的结果,将出现的错误摘录下来,并加上自己的解释;用自己的话总结确定派生类成员的访问属性的原则,也写到报告博文中。
C:\Documents and Settings\Administrator\桌面\Cpp1.cpp(64) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there i
不能输出string。前面应该调用string的函数
/** 程序的版权和版本声明部分* Copyright (c)2012, 烟台大学计算机学院学生* All rightsreserved.* 文件名称: x.cpp* 作者:徐本锡* 完成日期: 2013年5 月 28 日* 版本号: v1.0* 输入描述:* 问题描述:理解基类中成员的访问限定符和派生类的继承方式由下面派生类StudentB对基类StudentA的继承……* 程序输出:*///我的代码:#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;}//(1)尝试改变并运行// 请修改基类中成员的访问限定符和派生类的继承方式,考虑可能的运行结果或可能出现的错误,并在上机时进行验证、对比,达到理解派生类成员的访问属性的目的。//(2)总结// 记录(1)的结果,将出现的错误摘录下来,并加上自己的解释;用自己的话总结确定派生类成员的访问属性的原则,也写到报告博文中。
- 派生,继承1
- 继承与派生1
- 继承和派生1
- 继承与派生1
- 10-1 派生与继承
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 26785-1 继承与派生
- 5-1 继承与派生
- 继承与派生(1)
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 5-1 继承与派生
- 程序员职业规划发展路线图
- 导致CreateWindow创建窗口失败的一种原因
- 通过函数名字符串调用函数【C语言版】
- Maven实用命令
- POJ 3680 Intervals (最小费用最大流+离散化)
- 派生,继承1
- 怎样集中注意力
- 坑爹的excel中无法打开链接
- JQuery对数组的一些操作总结
- C语言 全局变量和局部变量的大小限制
- java解析xml的四种方式
- OpenCV2.4.4实现Harris角点检测
- JAVASCRIPT 浏览器兼容性问题及解决方案列表
- wifi启动信息跟踪之CRDA(1)