C++类的继承

这篇博客详细介绍了如何使用C++实现一个Teacher类,包含姓名、年龄、性别、电话和职称等属性。接着定义了一个Cadre类,增加了职务属性。最后,创建了一个Teacher_Cadre类,它同时继承了Teacher和Cadre,添加了工资属性,并实现了显示所有信息的show函数。在main函数中,通过实例化这三个类的对象并调用相应的显示函数,展示了类的使用。
摘要由CSDN通过智能技术生成

定义一个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<iostream>
    #include<string>
    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<<endl;
        cout << "年龄:" <<age<<endl;
        cout << "性别:" <<gender<<endl;
        cout << "电话:" <<tel<<endl;
        cout << "职称:" <<title<<endl;
        cout<<endl;
    }
    //Cadre::Cadre(){
    //	cout <<"Cadre请输入姓名、年龄、性别、电话、职务"<<endl;
    //    cin >>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 << "姓名:" <<name<<endl;
        cout << "年龄:" <<age<<endl;
        cout << "性别:" <<gender<<endl;
        cout << "电话:" <<tel<<endl;
        cout << "职称:" <<post<<endl;
        cout<<endl;
    }
    //Teacher_Cadre::Teacher_Cadre(){
    //	cout<<"请输入工资"<<endl;
    //    cin>>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<<"职务:"<<post<<endl;
        cout<<"工资:"<<wages<<endl;
        cout<<endl;
    }
    int main(void){
    //	Teacher t;
    //	Cadre c;
    //	Teacher_Cadre tc;
    //	t.display();
    //	c.display();
    //	tc.show();
    	Teacher t("张三", 21, "男", 111111, "高级教师");
    	Cadre c("李四", 30, "男", 666666, "教学秘书");
    	Teacher_Cadre tea_cad1("李明", 18, "女", 888888, "特级","研究生导师",30000);
    	t.display();
    	c.display();
    	tea_cad1.show();
    	return 0;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值