类的继承【多重继承】

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

class Student
{
private:
	string name;
	int age;
	int score;

public:
	Student() { cout << "学生:无参构造" << endl; }

	Student(string n, int a, int s) :name(n), age(a), score(s)
	{
		cout << "学生:有参构造" << endl;
	}

	~Student()
	{
		cout << "学生:析构函数" << endl;
	}

	void show()
	{
		cout << "Student::name = " << name << endl;
		cout << "Student::age = " << age << endl;
		cout << "Student::score = " << score << endl;
	}
};

class Party
{
private:
	string activity;
	string department;

public:
	Party() { cout << "党员:无参构造" << endl; }

	Party(string a, string d) :activity(a), department(d)
	{
		cout << "党员:有参构造" << endl;
	}

	~Party()
	{
		cout << "党员:析构函数" << endl;
	}

	void show()
	{
		cout << "Party::activity = " << activity << endl;
		cout << "Party::department = " << department << endl;
	}
};

class StuPart :public Student, public Party
{
private:
	string post;

public:
	StuPart() { cout << "新类:无参构造" << endl; }

	StuPart(string n, int a, int s, string aa, string d, string p) :Student(n, a, s), Party(aa, d), post(p)
	{
		cout << "新类:有参构造" << endl;
	}

	~StuPart()
	{
		cout << "新类:析构" << endl;
	}

	void show()
	{
		/*cout << "name = " << Student::name << endl;*/
		Student::show();  //调用父类的show函数
		Party::show();
		cout << "职位:" << post << endl;
	}
};

int main()
{
	StuPart stu1("张三", 18, 100, "学习新思想", "组织部", "部长");
	stu1.show();

	return 0;
}

运行结果

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值