c++篇——从零开始学类【四】
hello大家好,最近在做c++实验,就正好更新以下c++一大特色类的使用,主要通过实验来进行讲解,欢迎大家观看学习以及讨论~~
多继承与多类实现
本次内容
这次我们就开认认真真的接触继承还有多继承,还有对于多继承的处理等等了
在开始本次实验之前,大家可以先看看虚函数的用处
虚函数
这是本次实验的关键
实验目的
1.编写一个人员类,包含ID,姓名,出生年月,电话号码,联系地址等成员变量,以及输出函数。
2.编写教职工类,继承人员类,增加一个入职日期成员变量。
3.编写一个教师类,继承教职工类,增加一个职称成员变量。
4.编写一个行政人员类,继承教职工类,增加一个职务成员函数。
5.编写一个教师与行政类,继承教师类与行政人员类。
6.对于多重继承使用虚继承,避免出现多基类副本。
7.在人员类中增加一个静态成员函数“标题”,用于输出“编号,姓名,出生日期,电话,地址”等表格标题信息。
实验实现
在开始写之前我们可以先画个图
这是这次类与类之间的关系,我们需要想搞清楚每个类之间的关系才可以知道如何继承如何实现此次代码
那么开始吧
直接开始我们先把最简单的前四个类写了,这些是只需要继承父类的简单继承
【这不是最终代码,后续因为要实现虚继承,所以我们会在此上面进行修改】
class people{
public:
string id;
string name;
string day;
string add;
string number;
people(string id,string name,string day,string number,string add){
this->id=id;
this->name=name;
this->day=day;
this->number=number;
this->add=add;
}
static void biaoti(){
cout<<"编号"<<" 姓名"<<" 出生日期"<<" 电话号码"<<" 地址"<<" 入职日期"<<" 职务"<<endl;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<endl;
}
};
class worker:public people{
public:
string ruday;
worker(string id,string name,string day,string number,string add,string ruday):people(id,name,day,number,add){
this->ruday=ruday;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<ruday<<endl;
}
};
class teacher:public worker{
public:
string zhi;
teacher();
teacher(string id,string name,string day,string number,string add,string ruday,string zhi):worker(id,name,day,number,add,ruday){
this->zhi=zhi;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<" "<<ruday<<" "<<zhi<<endl;
}
};
class admin:public worker{
public:
string zhiwu;
admin(string id,string name,string day,string number,string add,string ruday,string zhiwu):worker(id,name,day,number,add,ruday){
this->zhiwu=zhiwu;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<" "<<ruday<<" "<<zhiwu<<endl;
}
};
这里补充一个小知识点,大家肯定注意到了我在每个类中都定义了相同的构造函数,如果这里每个类中不构造也是可以的,但是就是会出现除了人员类以外的每个类都是应用人员类中的show()函数,因为这是职工类的父类,而剩下两个教师类和行政类也是继承了职工类,也就是说教师类和行政类是人员类的孙子类,但是都开业继承人员类的所有公共内容,我们这里用到的也都是共有内容。
但是当我们在每个类里面都更新了相关构造函数之后在调用的时候我们会率先调用本函数中的函数
很不错,这里懂了,我们就往下,也就是本次的难点,虚继承,为什么呢,仔细观察图
会发现对于教师与行政类来说有一个问题就是因为他继承了两个不一样的类,但是这两个类中出现了一个重合的情况,啥意思?也就是说,教师与行政类中的姓名id出生日期等等是不是会出现重复呢,虽然对于我们来说我们知道重复我们自动忽略就可以了,但是计算机不知道啊,对于他来说教师类和行政类就是两个不一样的类,即便导入的数据是相同的,那么这个时候是不是我们就要考虑到虚继承了呢,也就是相当于这个时候我们回用到这两个单独类的同一个父类,职工类。
好啦大概理清楚之后我们就可以开始写代码了
class ta: public teacher, public admin{
public:
ta(string id,string name,string day,string number,string add,string ruday,string zhi,string zhiwu):teacher(id,name,day,number,add,ruday,zhi),admin(id,name,day,number,add,ruday,zhiwu),worker(id,name,day,number,add,ruday){
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<" "<<ruday<<" "<<zhi<<" "<<zhiwu<<endl;
}
};
这是教师与职工类的写法,千千万万要记住因为虚继承,所以我们这里不仅仅要在定义函数的时候用到teacher和admin还有worker也要加上哦
然后对于一开始的代码我们也要进行修改
class people{
public:
string id;
string name;
string day;
string add;
string number;
people(string id,string name,string day,string number,string add){
this->id=id;
this->name=name;
this->day=day;
this->number=number;
this->add=add;
}
static void biaoti(){
cout<<"编号"<<" 姓名"<<" 出生日期"<<" 电话号码"<<" 地址"<<" 入职日期"<<" 职务"<<endl;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<endl;
}
};
class worker:public people{
public:
string ruday;
worker(string id,string name,string day,string number,string add,string ruday):people(id,name,day,number,add){
this->ruday=ruday;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<ruday<<endl;
}
};
class teacher:virtual public worker{
public:
string zhi;
teacher();
teacher(string id,string name,string day,string number,string add,string ruday,string zhi):worker(id,name,day,number,add,ruday){
this->zhi=zhi;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<" "<<ruday<<" "<<zhi<<endl;
}
};
class admin:virtual public worker{
public:
string zhiwu;
admin(string id,string name,string day,string number,string add,string ruday,string zhiwu):worker(id,name,day,number,add,ruday){
this->zhiwu=zhiwu;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<" "<<ruday<<" "<<zhiwu<<endl;
}
};
欧克拉,大概修改成这个样子就可以了
然后我们来看完整代码吧
#include<iostream>
using namespace std;
class people{
public:
string id;
string name;
string day;
string add;
string number;
people(string id,string name,string day,string number,string add){
this->id=id;
this->name=name;
this->day=day;
this->number=number;
this->add=add;
}
static void biaoti(){
cout<<"编号"<<" 姓名"<<" 出生日期"<<" 电话号码"<<" 地址"<<" 入职日期"<<" 职务"<<endl;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<endl;
}
};
class worker:public people{
public:
string ruday;
worker(string id,string name,string day,string number,string add,string ruday):people(id,name,day,number,add){
this->ruday=ruday;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<ruday<<endl;
}
};
class teacher:virtual public worker{
public:
string zhi;
teacher();
teacher(string id,string name,string day,string number,string add,string ruday,string zhi):worker(id,name,day,number,add,ruday){
this->zhi=zhi;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<" "<<ruday<<" "<<zhi<<endl;
}
};
class admin:virtual public worker{
public:
string zhiwu;
admin(string id,string name,string day,string number,string add,string ruday,string zhiwu):worker(id,name,day,number,add,ruday){
this->zhiwu=zhiwu;
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<" "<<ruday<<" "<<zhiwu<<endl;
}
};
class ta: public teacher, public admin{
public:
ta(string id,string name,string day,string number,string add,string ruday,string zhi,string zhiwu):teacher(id,name,day,number,add,ruday,zhi),admin(id,name,day,number,add,ruday,zhiwu),worker(id,name,day,number,add,ruday){
}
void show(){
cout<<id<<" "<<name<<" "<<day<<" "<<add<<" "<<ruday<<" "<<zhi<<" "<<zhiwu<<endl;
}
};
int main(){
people *p;
worker *w;
teacher *t;
admin *a;
ta *ta1;
p=new people("001","wang","2000.1.1","11111","xian");
w=new worker("002","huang","2000.1.2","11112","guangzhou","2022.1.1");
t=new teacher("003","quan","2000.1.3","11113","beijing","2022.1.2","math");
a=new admin("004","yi","2000.1.4","11114","wuhu","2022.1.3","jiaowu");
ta1=new ta("005","chen","2000.1.5","11115","shanghai","2022.1.5","math","jiaowu");
p->biaoti();
p->show();
w->show();
t->show();
a->show();
ta1->show();
}
那么这样就完成啦!!
我们来看看最终运行结果
是不是就可以啦!你学废了嘛~~
下次见啦,我会持续更新的!!