#include <iostream> using namespace std; class Stu { public: Stu (int n,string nam); void display(); protected: int num; //学号 string name; //姓名 }; Stu::Stu(int n,string nam ) { num=n; name=nam; } void Stu::display() { cout<<"学号:"<<num<<endl; cout<<"姓名:"<<name<<endl; } class StuDetail:public Stu { public: StuDetail(int n,string nam,int a,string ad,int n1,string nam1); void show(); //成员函数,输出学生的信息 void show_monitor(); //成员函数,输出班长的信息 private: Stu monitor; //学生所在班的班长,班长是学生,是stu类的成员 int age; //学生年龄 string addr; // 学生的住址 }; StuDetail::StuDetail(int n,string nam,int a,string ad,int n1,string nam1): Stu(n,nam),monitor(n1,nam1),age(a),addr(ad){}; void StuDetail::show() { cout<<"学生信息:"<<endl; cout<<"学号:"<<num<<endl; cout<<"姓名:"<<name<<endl; cout<<"年龄:"<<age<<endl; cout<<"住址:"<<addr<<endl; } void StuDetail::show_monitor() { cout<<"班长信息:"<<endl; monitor.display(); } int main() { StuDetail s(10010,"wangli",19,"115 Beijing Road,shanghai",10001,"li-sun"); s.show(); s.show_monitor(); return 0; }
原文:http://blog.csdn.net/u012390928/article/details/25119345