作业:
#include <iostream>
using namespace std;
class Student //学生类
{
private: //私有权限
string name; //姓名
int age; //年龄
double score; //分数
public: //公共权限
Student() {cout<<"Student::无参构造"<<endl;} //无参构造
Student(string n,int a,double s):name(n),age(a),score(s) //有参构造
{
cout<<"Student::有参构造"<<endl;
}
//析构函数
~Student()
{
cout<<"Student::析构函数"<<endl;
}
//成员函数
void show()
{
cout<<"Student::name = "<<name<<endl;
cout<<"Student::age = "<<age<<endl;
cout<<"Student::score = "<<score<<endl;
}
};
class Party //党员类
{
private: //私有权限
string activity; //党组织活动
string organization; //组织
public: //公共权限
//无参构造
Party()
{cout<<"Party::无参构造"<<endl;}
//有参构造
Party(string ac,string org):activity(ac),organization(org)
{
cout<<"Party::有参构造"<<endl;
}
//析构函数
~Party()
{
cout<<"Party::析构函数"<<endl;
}
//成员函数
void show()
{
cout<<"Party::activity = "<<activity<<endl;
cout<<"Party::organization = "<<organization<<endl;
}
};
class Stuleader:public Student,public Party //派生出的学生干部类
{
private: //私有权限
string position;
public: //公共权限
//无参构造函数
Stuleader()
{
cout<<"Stuleader::无参构造"<<endl;
}
//有参构造函数
Stuleader(string n,int a,double s,string ac,string org,string p):Student(n,a,s),Party(ac,org),position(p)
{
cout<<"Stuleader::有参构造"<<endl;
}
//析构函数
~Stuleader()
{
cout<<"Stuleader::析构函数"<<endl;
}
//成员函数
void show()
{
this->Student::show(); //父子类同名,调用父类(Student)继承下来的成员
this->Party::show(); //父子类同名,调用父类(Party)继承下来的成员
cout<<"Stuleader::position = "<<position<<endl; //自己类中的私有成员,类内可以访问
}
};
int main()
{
Stuleader s1; //无参构造
Stuleader s2("aaa",18,98.5,"bbb","ccc","ddd"); //有参构造
s2.show();
return 0;
}