C++第四次实验

来源:互联网 发布:厦门众途软件 编辑:程序博客网 时间:2024/06/04 19:47
#include <iostream>    #include <string>    using namespace std;    class Stu   //声明基类    {    public:        Stu(int n, string nam );  //基类构造函数        void display( );          //成员函数,输出基类数据成员    private:        //访问权限为保护型的数据成员        int num;      //学生学号        string name;  //学生姓名    };    Stu::Stu(int n, string nam ): num(n),name(nam) {}  //基类构造函数        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); //派生类构造函数        void show( );        void show_monitor( );             //成员函数,输出子对象    private:        Stu monitor;   //学生所在班的班长,班长是学生,用Stu声明        int age;       //学生年龄        string addr;   //学生的住址    };        void Stu::display( )                               {        cout<<"学号: "<<num<<endl;        cout<<"姓名: "<<name<<endl;    }            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;        display();          cout<<"年龄: "<<age<<endl;        cout<<"住址: "<<addr<<endl<<endl;    }    void StuDetail::show_monitor( )    {        cout<<"班长信息:"<<endl;        monitor.display( );    }    int main( )    {        //学生张三,10010号,19岁,家住江西南昌,他的班长是李四,学号10001        StuDetail s(10010,"张三",19,"江西南昌",10001,"李四");        s.show( );                       //输出学生信息        s.show_monitor();                //输出班长信息        return 0;    }  

0 0
原创粉丝点击