c++第五次实验

来源:互联网 发布:java 反射获取泛型 编辑:程序博客网 时间:2024/05/17 22:43

一、问题及代码

/* *文件名称:1.cpp     *作者:曾魏伟     *完成日期:2016年5月20日     *对任务及求解方法的描述部分:     *输入描述:无     *问题描述:类的编写  *程序输出:按要求写出程序  *问题分析:略     *算法分析:略    */  #include<iostream>    #include<string>    using namespace std;    class Teacher  {        string title;    public:        void get(string name,int age,string sex,string title);//输入职称、名字、年龄、性别          void show_it();      string name;        int age;        string sex;    };    void Teacher::get(string name,int age,string sex,string title)    {        this->name=name;        this->age=age;        this->sex=sex;      this->title=title;    }    void Teacher::show_it()    {         cout<<"姓名:"<<name<<endl;        cout<<"年龄:"<<age<<endl;        cout<<"性别:"<<sex<<endl;        cout<<"职称:"<<title<<endl;    }    class Cadre:public Teacher    {        string post;    public:         void get1(string post);//输入职务        void showpost();   };    void Cadre::get1(string post)    {        this->post=post;    }    void Cadre::showpost()    {        Teacher::show_it();        cout<<"职务:"<<post<<endl;    }    class Teacher_Cadre:public Cadre    {        double wages;    public:         void get2(double wages);//输入工资        void show();//最终输出    };    void Teacher_Cadre::get2(double wages)    {       this->wages=wages;    }    void Teacher_Cadre::show()    {        Cadre::showpost();        cout<<"工资:"<<wages<<"元"<<endl;    }    int main()    {        Teacher_Cadre c;        c.get("曾辉",42,"男","副教授");          c.get1("主任");        c.get2(1534.5);        c.show();        return 0;    }    

二、运行结果


0 0
原创粉丝点击