当前位置:网站首页>Multiple inheritance virtual base exercises
Multiple inheritance virtual base exercises
2022-04-23 14:06:00 【JokerYourMemory】
Title From C++ primer plus After-school exercises 14-5
Code is divided into :
emp.h: The statement 4 Classes , They are virtual base classes abstr_emp,employee Class inherits this base class ,manager Virtual inheritance abstr_emp,fink Virtual inheritance abstr_emp,highfink multiple inheritance manager and employee.
emp.cpp: Provide function definitions .
main.cpp: Verification code .
#pragma once
#include<iostream>
#include<string>
using namespace std;
class abstr_emp
{
private:
string fname;
string lname;
string job;
public:
abstr_emp();
abstr_emp(const string& fn,const string& ln,const string& j);
virtual void ShowAll() const;
virtual void SetAll();
friend ostream& operator<<(std::ostream& os, const abstr_emp& e);
virtual ~abstr_emp() = 0 {};// Once declared, provide the definition , In this case, the method of inlining is adopted .
};
class employee : public abstr_emp
{
public:
employee();
employee(const string& fn, const string& ln, const string& j);
virtual void ShowAll() const;
virtual void SetAll();
};
class manager : virtual public abstr_emp {
private:
int inchargeof;
protected:
int InChargeOf() const{return inchargeof;}// Two Inchargeof Method , use protected type , Provide access to derived classes inchargeof The way
int & InChargeOf() { return inchargeof; }
public:
manager();
manager(const string& fn, const string& ln, const string& j,const int ico = 0);// Don't redefine the default values
manager(const abstr_emp&e,int ico);
manager(const manager& m);// Note that the function definition does not enter an endless loop , Call yourself
virtual void ShowAll() const;
virtual void SetAll();
};
class fink : virtual public abstr_emp {
private:
string reportsto;
protected:
const string ReportsTo() const { return reportsto; }
string& ReportsTo(){ return reportsto; }
public:
fink();
fink(const string& fn, const string& ln, const string& j,const string rpo);
fink(const abstr_emp& e,const string& rpo);
fink(const fink& e);
virtual void ShowAll() const;
virtual void SetAll();
};
class highfink : public manager, public fink {
public:
// Various construction methods depend on the constructor of the base class .
highfink(const string& fn, const string& ln, const string& j, const string rpo,int ico);
highfink();
highfink(const abstr_emp& e,const string &rpo,int ico);
highfink(const highfink &h);
highfink(const fink& f,int ico);
highfink(const manager& m,string rpo);
virtual void ShowAll() const;
virtual void SetAll();
};
emp.cpp:
#include"emp.h"
#include<iostream>
#include<string>
using namespace std;
ostream& operator<<(std::ostream& os, const abstr_emp& e)
{
os<< "frind of abs: first_name:[" << e.fname << "],last_name:[" << e.lname << "],job:[" << e.job << "]." << endl;
return os;
}
//abstr_emp class
abstr_emp::abstr_emp()
{
fname = "null";
lname = "null";
job = "null";
}
abstr_emp::abstr_emp(const string& fn, const string& ln, const string& j)
{
fname = fn;
lname = ln;
job = j;
}
void abstr_emp::ShowAll() const
{
cout << "abs: first_name:["<< fname << "],last_name:[" << lname <<"],job:["<<job<<"]."<< endl;
}
void abstr_emp::SetAll()
{
cout << "Input first name:";
cin >> fname;
cout << "\nInput last name:";
cin >> lname;
cout << "\nInput job:";
cin >> job;
cout << endl;
}
//employee class
employee::employee() :abstr_emp()
{
cout << "this is employ default instructor" << endl;
}
employee::employee(const string& fn, const string& ln, const string& j):abstr_emp(fn,ln,j){}
void employee::ShowAll() const
{
abstr_emp::ShowAll();
//((abstr_emp*)this)->ShowAll();// Writing like this will still cause the call to be employee Of showall To enter the dead cycle
}
void employee::SetAll()
{
abstr_emp::SetAll();
}
//manager class
manager::manager():abstr_emp(), inchargeof(5){}
manager::manager(const string& fn, const string& ln, const string& j, int ico ):abstr_emp(fn, ln, j), inchargeof(ico) {}
manager::manager(const abstr_emp&e, int ico) :abstr_emp(e), inchargeof(ico) {}
// Can this be omitted ?
manager::manager(const manager& m):abstr_emp(m),inchargeof(5){}
void manager::ShowAll() const
{
abstr_emp::ShowAll();
cout << "manager add: inchargeof[" << inchargeof << "]" << endl;
}
void manager::SetAll()
{
abstr_emp::SetAll();
cout << "fink requires inchargeof:";
cin >> inchargeof;
cout << endl;
}
//fink class overseer
fink::fink() :abstr_emp(), reportsto("null") {}
fink::fink(const string& fn, const string& ln, const string& j, const string rpo):abstr_emp(fn, ln, j), reportsto(rpo) {}
fink::fink(const abstr_emp& e, const string& rpo) :abstr_emp(e), reportsto(rpo) {}
fink::fink(const fink& e) : abstr_emp(e), reportsto(e.reportsto){}
void fink::ShowAll() const
{
abstr_emp::ShowAll();
cout << "fink add: reportsto[" << reportsto << "]" << endl;
}
void fink::SetAll()
{
abstr_emp::SetAll();
cout << "fink requires reportsto:";
cin >> reportsto;
cout << endl;
}
//highfink class
highfink::highfink(const string& fn, const string& ln, const string& j, const string rpo, int ico):\
abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, rpo) {}
highfink::highfink() :abstr_emp(), manager(), fink() {}
highfink::highfink(const abstr_emp& e, const string &rpo, int ico) : abstr_emp(e), manager(e, ico), fink(e, rpo) {}
highfink::highfink(const highfink& h) : abstr_emp(h), manager(h, h.manager::InChargeOf()), fink(h,h.fink::ReportsTo()) {}
highfink::highfink(const fink& f, int ico) : abstr_emp(f),fink(f),manager(f,ico){}//manager Whether it needs to be converted into a base class ?
highfink::highfink(const manager& m, string rpo) : abstr_emp(m), fink(m,rpo), manager(m) {}
void highfink::ShowAll() const
{
manager::ShowAll();
cout << "highfink adds ReportsTo:";
cout << fink::ReportsTo() << endl;
}
void highfink::SetAll()
{
manager::SetAll();
cout << "highfink acquires ReportsTo:";
cin>> fink::ReportsTo();
}
main.cpp:
// WineTest.cpp : Defines the entry point for the console application .
//
#include "stdafx.h"
#include <iostream>
#include "emp.h"
using namespace std;
int main()
{
employee em("Trip","Harris","Thumper");
cout << em << endl;
em.ShowAll();
manager ma("Amorphia", "Spindragon", "Nuancer", 5);
cout << ma << endl;
ma.ShowAll();
fink fi("Matt", "Oggs", "Oiler", "Juno Barr");
cout << endl;
fi.ShowAll();
highfink hf(ma,"Curly Kew");
hf.ShowAll();
cout << "Press a key for next phase:\n";
cin.get();
highfink hf2;
hf2.SetAll();
cout << "Using an abstr_emp * pointer:\n";
abstr_emp * tri[4] = { &em,&fi,&hf,&hf2 };
for (int i = 0; i < 4; i++)
tri[i]->ShowAll();
cin.get();
cin.get();
return 0;
}
Results output :
frind of abs: first_name:[Trip],last_name:[Harris],job:[Thumper].
abs: first_name:[Trip],last_name:[Harris],job:[Thumper].
frind of abs: first_name:[Amorphia],last_name:[Spindragon],job:[Nuancer].
abs: first_name:[Amorphia],last_name:[Spindragon],job:[Nuancer].
manager add: inchargeof[5]
abs: first_name:[Matt],last_name:[Oggs],job:[Oiler].
fink add: reportsto[Juno Barr]
abs: first_name:[Amorphia],last_name:[Spindragon],job:[Nuancer].
manager add: inchargeof[5]
highfink adds ReportsTo:Curly Kew
Press a key for next phase:
Input first name:Joe
Input last name:Cui
Input job:Secretary
fink requires inchargeof:20
highfink acquires ReportsTo:Alem
Using an abstr_emp * pointer:
abs: first_name:[Trip],last_name:[Harris],job:[Thumper].
abs: first_name:[Matt],last_name:[Oggs],job:[Oiler].
fink add: reportsto[Juno Barr]
abs: first_name:[Amorphia],last_name:[Spindragon],job:[Nuancer].
manager add: inchargeof[5]
highfink adds ReportsTo:Curly Kew
abs: first_name:[Joe],last_name:[Cui],job:[Secretary].
manager add: inchargeof[20]
highfink adds ReportsTo:Alem
版权声明
本文为[JokerYourMemory]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401090931.html
边栏推荐
- jsp学习1
- Cdh6 based on CM management 3.2 cluster integration atlas 2 one
- 可否把模板的头文件和源文件分开编译
- Wechat applet input hidden and inoperable settings
- 快速安装mongodb
- Restful WebService和gSoap WebService的本质区别
- 微信小程序基于udp协议与esp8266进行通信
- 微信小程序获取登录用户信息、openid和access_token
- As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture
- org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
猜你喜欢
CDH cluster integration Phoenix based on CM management
poi操作word模板替换数据并且导出word
程序编译调试学习记录
帆软报表设置单元格填报以及根据值的大小进行排名方法
分库分表 & ShardingSphere
Basic knowledge learning record
关于pthread多线程一些好文章
Detailed tutorial on the use of setinterval timing function of wechat applet
Qt Designer怎样加入资源文件
Intégration de Clusters CDH Phoenix basée sur la gestion cm
随机推荐
基于ibeacons签到系统
快捷键(多行)
微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
sql中出现一个变态问题
Ptorch classical convolutional neural network lenet
修改ddt生成的测试用例名称
smart-doc + torna生成接口文档
RobotFramework 之 用例标签机制
mysql新表,自增id长达20位,原因竟是......
帆软调用动态传参的方法,在标题中设置参数
Three point positioning based on ibeacons (wechat applet)
SPC简介
CDH cluster integration Phoenix based on CM management
STM32学习记录0007——新建工程(基于寄存器版)
linux MySQL数据定时dump
findstr不是内部或外部命令解决方法
微信小程序获取登录用户信息、openid和access_token
org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
Intégration de Clusters CDH Phoenix basée sur la gestion cm
Switch usage (wechat applet)