资讯详情

多重继承方式派生出新类

分别声明 Teacher(教师)类和Cadre(干部)这两类是通过多种继承方式衍生出来的 Teacher Cadre(教师兼干部)。 ①名称、年龄、性别、地址、电话等数据成员都包。 ②在 Teacher该类还包含数据成员 title(职称),在 Cadre该类还包含数据成员post(职位) Teacher Cadre该类还包含数据成员wages(工资)。 ③在引用这些数据成员时,指定两个基类中的姓名、年龄、性别、地址、电话等数据成员的作用域。 ④在类体中声明成员函数,在类外定义成员函数。 ⑤在派生类 Teacher Cadre的成员函数show中调用 Teacher类中的 display输出姓名、年龄、性别、职称、地址、电话号码cout语句输出职位和工资。 1.程序代码:

#include<iostream> #include<string> using namespace std; class teacher { 
        public:  teacher(string nam, int a, char s, string ad, int n, string t)  { 
          name = nam;   age = a;   sex = s;   addr = ad;   num = n;   title = t;  }  //void get_value();  void display(); protected:  string name;  int age;  char sex;  string addr;  int num;  string title; };  void teacher::display() { 
         cout << "name=" << name << endl;  cout << "age=" << age << endl;  cout << "sex=" << sex << endl;  cout << "addr=" << addr << endl;  cout << "num=" << num << endl;  cout << "title=" << title << endl;
}
class cadre
{ 
       
public:
	//cadre();
	cadre(string nam, int a, char s, string ad, int n, string p)
	{ 
       
		name = nam;
		age = a;
		sex = s;
		addr = ad;
		num = n;
		post = p;
	}
	//void get_value2();
protected:
	string name;
	int age;
	char sex;
	string addr;
	int num;
	string post;
};

class teacher_cadre:public teacher, public cadre
{ 
       public:
	//teacher_cadre() { name = 0, age = 0, sex = 0, addr = 0, num = 0, title = 0, post = 0, wages = 0; }
	teacher_cadre(string nam, int a, char s, string ad, int n,string t,string p, int w) :
		teacher(nam, a, s, ad, n,t), cadre(nam, a, s, ad, n,p),wages(w){ 
       }
	//void get_value1();
	void show();
private:
	int wages;
};

void teacher_cadre::show()
{ 
       
	cout << "post=" << cadre::post << endl;
	cout << "wages=" << wages << endl;
}
int main()
{ 
       
	teacher_cadre people1("zhang",12,'w',"beijing",12345678910,"hsu","sh",23);
	//cout << "输入这些值name,age,sex,addr,num,title,post,wages:" << endl;
	
	people1.teacher::display();
	people1.show();
	return 0;
}

2.运行结果: 在这里插入图片描述

标签: 进口数字温湿度传感器hsu

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台