2023/3/30 继承练习
定义一个学生类(Student):私有成员属性(姓名、年龄、分数)、成员方法(无参构造、有参构造、析构函数、show函数)
再定义一个党员类(Party):私有成员属性(党组织活动,组织),成员方法(无参构造、有参构造、析构函数、show函数)。
由这两个类共同派生出学生干部类,私有成员属性(职位),成员方法(无参构造、有参构造、析构函数、show函数),使用学生干部类实例化一个对象,然后调用其show函数进行测试
#include <iostream>
using namespace std;
class Stu
{
private:
string name;
int age;
double score;
public:
Stu() {}
Stu(string n,int a,double s):name(n),age(a),score(s)
{
cout << "Stu::有参构造" << endl;
}
~Stu(){cout << "Stu::析构函数" << endl;}
void show()
{
cout << "name = " << name << endl;
cout << "age = " << age << endl;
cout << "score = " << score << endl;
}
};
class Dang
{
private:
string active;
string organize;
public:
Dang() {}
Dang(string act,string org):active(act),organize(org)
{
cout << "Dang::有参构造" << endl;
}
~Dang(){cout << "Dang::析构函数" << endl;}
void show()
{
cout << "atcive = " << active << endl;
cout << "organize = " << organize << endl;
}
};
class StuG:public Stu,public Dang
{
private:
string work;
public:
StuG() {}
StuG(string n,int a,double s,string act,string org,string w):Stu(n,a,s),Dang(act,org),work(w)
{
cout << "StuG::有参构造" << endl;
}
~StuG(){cout << "StuG::析构函数" << endl;}
void show()
{
Stu::show();
Dang::show();
cout << "work = " << work << endl;
}
};
int main()
{
StuG s1("zhang",18,1.23,"党员","共青团","书记");
cout << "===========================" << endl;
s1.show();
cout << "===========================" << endl;
return 0;
}