当前位置:网站首页>多重继承虚基类习题
多重继承虚基类习题
2022-04-23 14:01:00 【JokerYourMemory】
题目自C++ primer plus 课后习题14-5
代码分为:
emp.h:声明了4个类,分别是虚基类abstr_emp,employee类继承此基类,manager虚继承abstr_emp,fink虚继承abstr_emp,highfink多重继承manager和employee。
emp.cpp:提供函数定义。
main.cpp:验证代码。
#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 {};//声明了就要提供定义,在此采用内联的方式。
};
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;}//两个Inchargeof方法,采用protected类型,为派生类提供访问inchargeof的方式
int & InChargeOf() { return inchargeof; }
public:
manager();
manager(const string& fn, const string& ln, const string& j,const int ico = 0);//默认值不要重定义
manager(const abstr_emp&e,int ico);
manager(const manager& m);//注意函数定义不要进入死循环,即自己调用自己
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:
//各种构造方式都依赖基类的构造函数。
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类
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类
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();//这样写会导致调用的仍然是employee的showall从而进入死循环
}
void employee::SetAll()
{
abstr_emp::SetAll();
}
//manager类
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) {}
//这种能否省略呢?
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类 监工
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类
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是否需要转换成基类?
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 : 定义控制台应用程序的入口点。
//
#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;
}
结果输出:
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://blog.csdn.net/m0_38084180/article/details/80588498
边栏推荐
- Choreographer full resolution
- Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
- Yarn online dynamic resource tuning
- L2-024 tribe (25 points)
- Wechat applet
- Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
- 金蝶云星空API调用实践
- 淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
- Kettle--控件解析
- 分库分表 & ShardingSphere
猜你喜欢
分库分表 & ShardingSphere
About note 1
Taobao released the baby prompt "your consumer protection deposit is insufficient, and the expiration protection has been started"
神经元与神经网络
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
Interesting talk about network protocol
CentOS mysql多实例部署
趣谈网络协议
Oracle alarm log alert Chinese trace and trace files
nodejs通过require读取本地json文件出现Unexpected token / in JSON at position
随机推荐
3300万IOPS、39微秒延迟、碳足迹认证,谁在认真搞事情?
What is the difference between blue-green publishing, rolling publishing and gray publishing?
L2-024 tribe (25 points)
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
Express②(路由)
项目中遇到的问题(五)操作Excel接口Poi的理解
JS brain burning interview question reward
服务器中挖矿病毒了,屮
Special test 05 · double integral [Li Yanfang's whole class]
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
PySide2
Android interview theme collection
VsCode-Go
scikit-learn构建模型的万能模板
Reading notes: Secure federated matrix factorization
MySQL 修改主数据库
Analysis and understanding of atomicintegerarray source code
神经元与神经网络
Program compilation and debugging learning record