#include <iostream>
using namespace std;
//学生类
class Stu
{
private:
string name;
int age;
double score;
public:
//无参构造
Stu()
{cout << "Stu::无参构造" << endl;}
//有参构造
Stu(string n,int a,double s):name(n),age(a),score(s)
{cout << "Stu::有参构造" << endl;}
//析构函数
~Stu() {}
//展示
void show()
{
cout << "name = " << name << " age = " << age << " score = " << score << endl;
}
};
//党员类
class Party
{
private:
string activity;
string organization;
public:
//无参构造
Party()
{cout << "Party::无参构造" << endl;}
//有参构造
Party(string act,string org):activity(act),organization(org)
{cout << "Party::有参构造" << endl;}
//析构函数
~Party() {}
void show()
{
cout << "activity = " << activity << " organization = " << organization << endl;
}
};
//学生干部
class Leader:public Stu,public Party
{
private:
string postion;
public:
//无参构造
Leader():Stu("孙凡凡",18,89),Party("采访","新闻部")
{cout << "Leader::无参构造" << endl;}
//有参构造
Leader(string n,int a,double s,string act,string org,string pos):Stu(n,a,s),Party(act,org),postion(pos)
{cout << "Leader::有参构造" << endl;}
//析构函数
~Leader() {}
//展示
void show()
{
cout << "postion = " << postion << endl;
}
};
int main()
{
Leader s1;
Leader s2;
s2.Stu::show();
s2.Party::show();
Leader s3("邓金宝",19,98,"支部大会","卫生部","部长");
s3.Stu::show();
s3.Party::show();
s3.show();
return 0;
}
4/23作业继承
于 2023-04-24 00:31:41 首次发布
该代码示例展示了C++中如何使用类来表示学生、党员和学生干部,包括无参构造、有参构造和析构函数。学生类(Stu)包含姓名、年龄和分数属性,党员类(Party)包含活动和组织属性,而学生干部类(Leader)继承自Stu和Party,添加了职位属性。在main函数中创建并展示了不同对象的属性。
摘要由CSDN通过智能技术生成