作业:
封装一个学生类(Student):包括受保护成员:姓名、年龄、分数,完成其相关函数,以及show
在封装一个领导类(Leader):包括受保护成员:岗位、工资,完成其相关函数及show
由以上两个类共同把派生出学生干部类:引入私有成员:管辖人数,完成其相关函数以及show函数
在主程序中,测试学生干部类:无参构造、有参构造、拷贝构造、拷贝赋值、show。
头文件:
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
//学生类
class Stu
{
protected:
string name; //姓名
int age; //年龄
double score; //分数
public:
Stu(); //无参构造
Stu(string m,int n,double k); //有参构造
Stu(const Stu &other); //拷贝构造
Stu &operator=(const Stu &other); //拷贝赋值
void show(); //打印展示函数
~Stu(); //析构函数
};
//领导类
class Leader
{
protected:
string post; //岗位
double wage; //工资
public:
Leader(); //无参构造
Leader(string m,double n); //you有参构造
Leader(const Leader &other); //拷贝构造
Leader &operator=(const Leader &other); //拷贝赋值
void show(); //打印展示函数
~Leader(); //析构函数
};
//学生干部类
class studentcadre:public Stu,public Leader
{
private:
int people;
public:
studentcadre(); //无参构造
studentcadre(string a,int b,double c,string d,double e,int f); //有参构造
studentcadre(const studentcadre &other); //拷贝构造
studentcadre &operator=(const studentcadre &other); //拷贝赋值
void show(); //打印展示函数
~studentcadre(); //析构函数
};
#endif // HEADER_H
源文件:
#include <iostream>
#include <cstring>
#include <string>
#include "header.h"
using namespace std;
//无参构造
Stu::Stu()
{
cout<<"无参构造"<<endl;
}
//有参构造
Stu::Stu(string m,int n,double k):name(m),age(n),score(k)
{
cout<<"有参构造"<<endl;
}
//拷贝构造
Stu::Stu(const Stu &other):name(other.name),age(other.age),score(other.score)
{
cout<<"拷贝构造"<<endl;
}
//拷贝赋值
Stu &Stu::operator=(const Stu &other)
{
if(this != &other)
{
this->name = other.name;
this->age = other.age;
this->score = other.score;
}
cout<<"拷贝赋值"<<endl;
return *this;
}
//打印展示函数
void Stu::show()
{
cout<<"name = "<<name<<endl;
cout<<"age = "<<age<<endl;
cout<<"score = "<<score<<endl;
}
//析构函数
Stu::~Stu()
{
cout<<"析构函数"<<endl;
}
//无参构造
Leader::Leader()
{
cout<<"无参构造"<<endl;
}
//有参构造
Leader::Leader(string m,double n):post(m),wage(n)
{
cout<<"有参构造"<<endl;
}
//拷贝构造
Leader::Leader(const Leader &other)
{
post = other.post;
wage = other.wage;
cout<<"拷贝构造"<<endl;
}
//拷贝赋值
Leader &Leader::operator=(const Leader &other)
{
if(this != &other)
{
this->post = other.post;
this->wage = other.wage;
}
cout<<"拷贝赋值"<<endl;
return *this;
}
//打印展示函数
void Leader::show()
{
cout<<"post = "<<post<<endl;
cout<<"wage = "<<wage<<endl;
}
//析构函数
Leader::~Leader()
{
cout<<"析构函数"<<endl;
}
//无参构造
studentcadre::studentcadre()
{
cout<<"无参构造"<<endl;
}
//有参构造
studentcadre::studentcadre(string a,int b,double c,string d,double e,int f):Stu(a,b,c),Leader(d,e),people(f)
{
cout<<"有参构造"<<endl;
}
//拷贝构造
studentcadre::studentcadre(const studentcadre &other):Stu(other),Leader(other),people(other.people)
{
cout<<"拷贝构造"<<endl;
}
//拷贝赋值
studentcadre &studentcadre::operator=(const studentcadre &other)
{
if(this != &other)
{
Stu::operator=(other);
Leader::operator=(other);
people = other.people;
}
cout<<"拷贝赋值"<<endl;
return *this;
}
//打印展示函数
void studentcadre::show()
{
cout<<"Stu.name = "<<name<<endl;
cout<<"Stu.age = "<<age<<endl;
cout<<"Stu.score = "<<score<<endl;
cout<<"Leader.post = "<<post<<endl;
cout<<"Leader.age = "<<wage<<endl;
cout<<"people = "<<people<<endl;
}
//析构函数
studentcadre::~studentcadre()
{
cout<<"析构函数"<<endl;
}
主函数:
#include <iostream>
#include <cstring>
#include <string>
#include "header.h"
using namespace std;
int main()
{
Stu s1;
Stu s2("ycf",25,150);
Stu s3 = s2;
s2.show();
Leader s4;
Leader s5("嵌入式",15000);
Leader s6 = s5;
s2.show();
studentcadre s7;
studentcadre s8("pp",26,149,"单片机",12345,10);
studentcadre s9;
s9 = s8;
s9.show();
return 0;
}
代码执行结果: