当前位置:网站首页>多重继承虚基类习题
多重继承虚基类习题
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
边栏推荐
- Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
- Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
- 商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
- 3300万IOPS、39微秒延迟、碳足迹认证,谁在认真搞事情?
- 神经元与神经网络
- Android 面试主题集合整理
- Redis docker 安装
- Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
- 快捷键(多行)
- Nodejs安装及环境配置
猜你喜欢

STM32 learning record 0007 - new project (based on register version)

2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析

Ptorch classical convolutional neural network lenet

Decentralized Collaborative Learning Framework for Next POI Recommendation

crontab定时任务输出产生大量邮件耗尽文件系统inode问题处理

Wechat applet

Oracle alarm log alert Chinese trace and trace files

JMeter pressure test tool

go 语言 数组,字符串,切片

专题测试05·二重积分【李艳芳全程班】
随机推荐
[code analysis (2)] communication efficient learning of deep networks from decentralized data
[code analysis (5)] communication efficient learning of deep networks from decentralized data
UNIX final exam summary -- for direct Department
JS brain burning interview question reward
关于stream流,浅记一下------
STM32 learning record 0007 - new project (based on register version)
Strange bug of cnpm
The art of automation
leetcode--977. Squares of a Sorted Array
[VMware] address of VMware Tools
第一章 电商秒杀商品回顾
Modify the Jupiter notebook style
基础知识学习记录
CentOS mysql多实例部署
YARN线上动态资源调优
Android interview theme collection
Android 面试主题集合整理
_模_板_
对List集合进行分页
剑南春把文字游戏玩明白了