C++第4次作业
2016-04-22 10:42
465 查看
#include <iostream> #include <string> using namespace std; class Person{ public: Person(string s){ name=s; } void display( ){ cout<<"Name: "<<name<<endl; } private: string name; }; class Student:public Person//(1)参考教材P169加底纹部分 { public: Student(string s, int g):Person(s) // (2) {grade=g;} void display1( ) { display();// (3) cout<<"Grade: "<<grade<<endl; } private: int grade; }; int main( ) { Student s("龙三",19); s.display1(); // (4) return 0; } #include <iostream> #include <string> using namespace std; class Stu //声明基类 { public: Stu(int n, string nam ):num(n),name(nam){}//基类构造函数 void display( ) { cout<<"学号:"<<num<<endl; cout<<"姓名:"<<name<<endl; }//成员函数,输出基类数据成员 protected: //(*)访问权限为保护型的数据成员 int num; //学生学号 string name; //学生姓名 }; class StuDetail: public Stu //声明派生类StuDetail { public: //学生nam,学号n,a岁,家住ad,他的班长是nam1,学号n1 StuDetail(int n, string nam,int a, string ad,int n1, string nam1):Stu(n,nam),age(a),addr(ad),monitor(n1,nam1){} //派生类构造函数 void show( ) { cout<<"学生信息"<<":"<<endl; display(); cout<<"年龄:"<<age<<endl; cout<<"地址:"<<addr<<endl<<endl; }//成员函数,输出学生的信息 void show_monitor( ) { cout<<"班长信息:"<<endl; monitor.display(); }//成员函数,输出班长信息 private: Stu monitor; //学生所在班的班长,班长是学生,是Stu类的成员 int age; //学生年龄 string addr; //学生的住址 }; int main( ) { //学生张三,10010号,19岁,家住江西南昌,他的班长是李四,学号10001 StuDetail s(10010,"张三",19,"江西南昌",10001,"李四"); s.show( ); //输出学生信息 s.show_monitor(); //输出班长信息 return 0; }
相关文章推荐
- c++第二次作业
- C++第三次作业
- Dev-C++使用注意点
- C++001--字符串
- c++第四次上机实验——学生类
- C++第一次实验1-2
- 多线程:C++11 multithreading
- C++第四次作业
- C++第一次实验1-1
- C++实验3-3
- C++ 第二次上机实验
- C++实验3-1
- C语言事实上不简单:sizeof
- C++实验3-2
- 广义表的递归实现
- 山东省第五届ACM大学生程序设计竞赛-Hearthstone II(组合数学-第二类Stirling数)
- Stirling数-组合数学
- [c++]百度笔试题
- 20160421 VC++6.0环境下gSOAP的使用
- Android JNI: Can't include C++ headers like vector or string...