#include <iostream>
using namespace std;
//学生类
class Student
{
protected:
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(const Student &other):name(other.name),age(other.age),score(other.score)
{
cout<<"Student::拷贝构造函数"<<endl;
}
//拷贝赋值函数
Student &operator=(const Student &other)
{
if(this != &other)
{
this->name = other.name;
this->age = other.age;
this->score = other.score;
}
cout<<"Student::拷贝赋值函数"<<endl;
return *this;
}
//析构函数
~Student()
{
cout<<"Student::析构函数"<<endl;
}
//show函数
void show()
{
cout<<"name = "<<name<<endl;
cout<<"age = "<<age<<endl;
cout<<"score = "<<score<<endl;
}
};
//领导类
class Leader
{
protected:
string post;
int salary;
public:
//无参构造
Leader(){cout<<"Leader::无参构造"<<endl;}
//有参构造
Leader(string p,int s):post(p),salary(s)
{
cout<<"Leader::有参构造"<<endl;
}
//拷贝构造函数
Leader(const Leader &other):post(other.post),salary(other.salary)
{
cout<<"Leader::拷贝构造函数"<<endl;
}
//拷贝赋值函数
Leader &operator=(const Leader &other)
{
if(this != &other)
{
this->post = other.post;
this->salary = other.salary;
}
cout<<"Leader::拷贝赋值函数"<<endl;
return *this;
}
//析构函数
~Leader()
{
cout<<"Leader::析构函数"<<endl;
}
//show函数
void show()
{
cout<<"post = "<<post<<endl;
cout<<"salary = "<<salary<<endl;
}
};
//派生的学生干部类
class Stu_leader:public Student,public Leader
{
private:
int num; //管理人数
public:
//无参构造
Stu_leader(){cout<<"Stu_leader::无参构造"<<endl;}
//有参构造
Stu_leader(string n,int a,double s,string p,int sa,int nu):Student(n,a,s),Leader(p,sa),num(nu)
{
cout<<"Stu_leader::有参构造"<<endl;
}
//拷贝构造函数
Stu_leader(const Stu_leader &other):Student(other),Leader(other)
{
cout<<"Stu_leader::拷贝构造函数"<<endl;
}
//拷贝赋值函数
Stu_leader &operator=(const Stu_leader &other)
{
if(this != &other)
{
this->num = other.num;
//调用学生类拷贝赋值函数
Student::operator=(other);
//调用领导类拷贝赋值函数
Leader::operator=(other);
}
cout<<"Stu_leader::拷贝赋值函数"<<endl;
return *this;
}
//析构函数
~Stu_leader()
{
cout<<"Stu_leader::析构函数"<<endl;
}
//show函数
void show()
{
cout<<"Student::name = "<<name<<endl;
cout<<"Student::age = "<<age<<endl;
cout<<"Student::score = "<<score<<endl;
cout<<"Lead::post = "<<post<<endl;
cout<<"Lead::salary = "<<salary<<endl;
cout<<"Stu_leader::num = "<<num<<endl;
}
};
int main()
{
Student s1("csy",23,95);
Student s2 = s1;
Student s3;
s3 = s1;
s1.show();
cout<<endl;
Leader l1("校长",5000);
Leader l2 = l1;
Leader l3;
l3 = l1;
l1.show();
l2.show();
l3.show();
Stu_leader p1("yy",22,99,"学生会主席",5000,12);
p1.show();
Stu_leader p2 = p1;
p2.show();
Stu_leader p3;
p3 = p1;
p3.show();
return 0;
}
执行结果