栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++类的继承

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++类的继承

定义一个Teacher类,类中包括:

数据成员:name(姓名), age(年龄), gender(性别), tel(电话), title(职称)。

函数成员:构造函数。display函数输出对象的数据。

定义一个Cadre类,类中包括:

数据成员:name(姓名), age(年龄), gender(性别), tel(电话), post(职务)。

函数成员:构造函数。display函数输出对象额数据。

定义一个Teacher_Cadre类,公用继承Teacher类,公用接触Cadre类。类中包括:

数据成员:wages(工资)。

函数成员:构造函数。show函数输出对象的数据。

要求:

  1. 类的成员函数类内声明,类外实现。
  2. 派生类中的同名成员,在引用时,指定作用域。
  3. Teacher_Cadre类的成员函数show中,调用Teacher函数中的display,在用cout语句输出post(职务)和wages(工资)。
  4. 在main函数中定义三个类的对象,并使用成员函数输出对象的数据。
    #include
    #include
    using namespace std;
    class Teacher{
    	private:
    	 	string name;
    	 	int age;
    		string gender;
    	 	int tel;
    		 string title;
    	 public:
    	 	Teacher(string,int,string,int,string);
    		void display();
    };
    class Cadre{
    	protected:
    		string post;
    		string name;
    		int age;
    		string gender;
    		int tel; 
    	public:
    		Cadre(string,int,string,int,string);
    		void display();
    };
    class Teacher_Cadre:public Teacher,public Cadre{
    	private:
    		double wages;
    	public:
    		Teacher_Cadre(string, int, string, int, string, string, float);
    		void show();
    };
    
    //Teacher::Teacher(){
    //    cout <<"Teacher请输入姓名、年龄、性别、电话、职称" << endl;
    //    cin >>name>>age>>gender>>tel>>title;
    //}
    Teacher::Teacher(string n, int a, string g, int te, string ti) {
    	name = n;
    	age = a;
    	gender = g;
    	tel = te;
    	title = ti;
    }
    void Teacher::display(){
    	cout << "姓名:" <name>>age>>gender>>tel>>post;
    //}
    Cadre::Cadre(string n, int a, string g, int t, string p):name(n),age(a),gender(g),tel(t),post(p){}
    void Cadre::display(){
    	cout << "姓名:" <wages;
    //}
    Teacher_Cadre::Teacher_Cadre(string n, int a, string g, int t, string ti, string p, float w):Teacher(n,a,g,t,ti),Cadre(n,a,g,t,p),wages(w){}
    void Teacher_Cadre::show(){
    	Teacher::display();
        cout<<"职务:"< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/947468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号