C++ 多重继承

 作业
1.定义一个人类(Human):私有成员(姓名,年龄)、成员方法(无参构造、有参构造、析构函数、show函数)
2.由人类派生出一个学生类(Student):私有成员属性(分数)、成员方法(无参构造、有参构造、析构函数、show函数)
3.由人类派生出一个党员类(Party):私有成员属性(党组织活动,组织),成员方法(无参构造、有参构造、析构函数、show函数)。
4.由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,
然后调用其show函数进行测试

#include <iostream>
using namespace std;

class Human{
	public:
		//无参构造
		Human(){}
		//有参构造
		Human(string n , int a):name(n),age(a){}
		//析构函数
		~Human(){}
		//show函数
		void show(){
			cout<<"name= "<<name<< " age= "<<age<<endl;
		}
	private:
		string name;
		int age;
		
};
class Student:virtual public Human{
		public:
			//无参构造
			Student(){}
			//有参构造
			Student(string n , int a,float s):Human(n,a),score(s){}
			//析构函数
			~Student(){}
			//show函数
			void show(){
				cout<<"score= "<<score<<endl;
			}
	private:
		float score;
};
class Party:virtual public Human{
		public:
			//无参构造
			Party(){}
			//有参构造
			Party(string n , int a,string m,string o):Human(n,a),maneuver(m),organization(o){}
			//析构函数
			~Party(){}
			//show函数
			void show(){
				cout<<"maneuver= "<<maneuver<<" organization= "<<organization<<endl;
			}
	private:
		string maneuver;
		string organization;
};

class Stu_Party:public Party,public Student{
	public:
			//无参构造
			Stu_Party(){}
			//有参构造
			Stu_Party(string n , int a,float s ,string m,string o,string p):Student(n,a,s),Party(n,a,m,o),position(p){}
			//析构函数
			~Stu_Party(){}
			//show函数
			void show(){
				Party&t=*this;
				t::Human::show();
				Party::show();
				Student::show();
				cout<<"position= "<<position<<endl;
			}
	private:
		string position;
};

int main(){
	Stu_Party s1("test",18,99.1,"阿巴","???","ok");
	s1.show();
	return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值