C++primer plus第六版课后编程题答案14.5

emp.cpp

#include <iostream>
#include <string>
using namespace std;
static void eatLine()
{
	while(cin.get()!='\n')
		continue;
};
class emp
{
private:
	string fname;
	string lname;
	string job;
public:
	emp(){
		fname="fdefault";
		lname="ldefault";
		job="jdefault";
	}
	emp(const string &fn,const string &ln,const string &j){
		fname=fn;
		lname=ln;
		job=j;
	}
	virtual void speciality()const=0
	{
		cout<<"this is emp:"<<endl;
	}
	virtual void showAll()const
	{
		speciality();//用于区分
		cout<<"fullname:"<<fname<<"     lastname:"<<lname<<endl;
	}
	virtual void setAll()
	{
		speciality();
		cout<<"Please enter the fullname:"<<endl;
		string ffname;
		getline(cin,ffname);
		eatLine();
		cout<<"Please enter the lastname:"<<endl;
		string llname;
		getline(cin,llname);
		eatLine();
	}
	friend ostream&operator<<(ostream &os,const emp &e)
	{
		e.showAll();//调用成员函数搞定
		return os;
	}
	virtual ~emp(){}
};

class employee:public emp
{
public:
	employee(){};
	employee(const string &fn,const string &ln,const string &j):emp(fn,ln,j){};
	virtual void speciality()const
	{
		cout<<"this is employee:"<<endl;
	}
	virtual void setAll()
	{
		emp::setAll();
	}
	virtual void showAll()const
	{
		//speciality();//这里无需调用,因为上一层已经调用过
		emp::showAll();
	}
};

class manager:virtual public emp
{
private:
	int inchargeof;
protected:
	int InChargeOf()const{return inchargeof;};
	int &InChargeOf(){return inchargeof;};
public:
	manager(){};
	manager(const string &fn,const string &ln,const string &j,int c=0):emp(fn,ln,j),inchargeof(c){};
	manager(const emp &e,int ico):emp(e),inchargeof(ico){};
	manager(const manager &m):emp(m){
		inchargeof=m.inchargeof;
	}
	virtual void speciality()const
	{
		cout<<"This is manager:"<<endl;
	}
	virtual void showAll()const{
		emp::showAll();
		cout<<"inchargeof:"<<inchargeof<<endl;

	};
	virtual void setAll()
	{
		emp::setAll();
		cout<<"Enter the inchargeof:";
		//int n;
		cin>>inchargeof;
		eatLine();
	}
};

class fink:virtual public 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):emp(fn,ln,j),reportsto(rpo){}
	fink(const emp &e,const string &rpo):emp(e),reportsto(rpo){}
	fink(const fink &e):emp(e)
	{
		reportsto=e.reportsto;
	}
	virtual void speciality()const
	{
		cout<<"this is fink:"<<endl;
	}
	virtual void showAll()const
	{
		emp::showAll();
		cout<<"Reportsto:"<<reportsto<<endl;
	}
	virtual void setAll()
	{
		emp::setAll();
		cout<<"Enter the reportsto:";
		//int n;
		getline(cin,reportsto);
		eatLine();
		eatLine();
	}
};

class highfink:public manager,public fink
{
public:
	highfink(){};
	highfink(const string &fn,const string &ln,const string &j,const string &rpo,int ico)
		:emp(fn,ln,j),manager(fn,ln,j,ico),fink(fn,ln,j,rpo){}
	highfink(const emp &e,const string &rpo,int ico)
		:emp(e),manager(e,ico),fink(e,rpo){}//注意这个构造是怎么构造的
	highfink(const fink &f,int ico)
		:emp(f),fink(f),manager(f,ico){}//这样行不行呢?
	highfink(const manager &m,const string &rpo)
	:emp(m),manager(m),fink(m,rpo){}

	highfink(const highfink &h)
		:emp(h),manager(h),fink(h){}//这样可否??

	virtual void speciality()const//const必须与emp一致,否则会隐藏原来的,而不是重载
	{
		cout<<"This is highfink:"<<endl;
	}
	virtual void showAll()const//注意大小写不要写错了
	{
		//out<<"here is !"<<endl;
		emp::showAll();
		manager::showAll();
		fink::showAll();
	}
	virtual void setAll()
	{
		manager::setAll();
		fink::setAll();
	}
};

main145.cpp

#include <iostream>
#include "emp.cpp"
using namespace std;
void main145()
{
	/*
	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","Olier","Juno Barr");
	//cout<<fi<<endl;
	//fi.showAll();

	//highfink hf(ma,"Curly kew");
	highfink hf(fi,50);
	hf.showAll();//这个似乎有点问题
					//直接跳到了manager那里的showAll(),什么情况?//原来是我的show大小没注意
					//格式你们自己搞
	*/
	
	cout<<"Press a key for next phase:"<<endl;
	cin.get();
	//懒得写了,吃饭去
	highfink hf2;
	hf2.setAll();

	cout<<"Using an emp *point to it :"<<endl;
	
	hf2.showAll();
	
	cin.get();


}

--------------------------------------------------------------分界线-----------------------------------------------

第17章有道题要用这个,于是回去发现了bug所在,新的在这里

ee.cpp

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
static enum{Emp,Employee,Manager,Fink,Highfink};
//不能写成小写的emp,否则在main175中无法创建对应的对象
static void eatLine()
{
	while(cin.get()!='\n')
		continue;
};
class emp
{
private:

	string fname;
	string lname;
	string job;
public:
	emp(){
		fname="fdefault";
		lname="ldefault";
		job="jdefault";
	}
	emp(const string &fn,const string &ln,const string &j){
		fname=fn;
		lname=ln;
		job=j;
	}

	virtual void Data(ofstream &fout){//标记
		fout<<Emp<<endl;
	};
	virtual void save(ofstream &fout){
		Data(fout);
		fout<<fname<<endl;
		fout<<lname<<endl;
		fout<<job<<endl;

	};//保存到文本
	//读取文本
	virtual void getall(ifstream &fin)
	{
		fin>>fname>>lname>>job;
	}



	virtual void speciality()const=0
	{
		cout<<"this is emp:"<<endl;
	}
	virtual void showAll()const
	{
		speciality();//用于区分
		cout<<"fullname:"<<fname<<"     lastname:"<<lname<<"  job:"<<job<<endl;
	}
	virtual void setAll()
	{
		//eatLine();//清除前面的输入
		speciality();
		cout<<"\nPlease enter the fullname:";
		//string ffname;//应该直接赋值给fname
		//getline(cin,fname);
		//eatLine();
		cin>>fname;
		cout<<"\nPlease enter the lastname:";
		//string llname;
		//getline(cin,lname);
		cin>>lname;
		cout<<"\nPlease enter the job:";
		//string llname;
		//getline(cin,job);
		cin>>job;
		//eatLine();
	}
	friend ostream&operator<<(ostream &os,const emp &e)
	{
		e.showAll();//调用成员函数搞定
		return os;
	}
	virtual ~emp(){}
};

class employee:public emp
{
public:
	employee(){};
	employee(const string &fn,const string &ln,const string &j):emp(fn,ln,j){};
	virtual void speciality()const
	{
		cout<<"this is employee:"<<endl;
	}
	virtual void setAll()
	{
		emp::setAll();
	}
	virtual void showAll()const
	{
		//speciality();//这里无需调用,因为上一层已经调用过
		emp::showAll();
	}
	virtual void Data(ofstream &fout){//标记
		fout<<Employee<<endl;
	};
	virtual void save(ofstream &fout){
		emp::save(fout);//没有新元素
	};//保存到文本
	virtual void getall(ifstream &fin)
	{
		emp::getall(fin);
		//fin>>fname>>lname>>job;
	}


};

class manager:virtual public emp
{
private:
	int inchargeof;
protected:
	int InChargeOf()const{return inchargeof;};
	int &InChargeOf(){return inchargeof;};
	void setI(int i)//设置inchargeof
	{
		inchargeof=i;
	}
public:
	manager(){};
	manager(const string &fn,const string &ln,const string &j,int c=0):emp(fn,ln,j),inchargeof(c){};
	manager(const emp &e,int ico):emp(e),inchargeof(ico){};
	manager(const manager &m):emp(m){
		inchargeof=m.inchargeof;
	}
	virtual void speciality()const
	{
		cout<<"This is manager:"<<endl;
	}
	virtual void showAll()const{
		emp::showAll();
		cout<<"inchargeof:"<<inchargeof<<endl;

	};
	virtual void setAll()
	{
		emp::setAll();
		cout<<"Enter the inchargeof:";
		//int n;
		cin>>inchargeof;
		eatLine();
	}
	virtual void Data(ofstream &fout){//标记
		fout<<Manager<<endl;
	};
	virtual void save(ofstream &fout){
		emp::save(fout);
		fout<<inchargeof<<endl;

	};//保存到文本
	virtual void getall(ifstream &fin)
	{
		emp::getall(fin);
		fin>>inchargeof;
		//fin>>fname>>lname>>job;
	}
};

class fink:virtual public emp
{
private:
	string reportsto;
protected:
	const string ReportsTo()const {return reportsto;};
	string &ReportsTo(){return reportsto;};
	void setRe(string str)
	{
		reportsto=str;
	}
public:
	fink(){};
	fink(const string &fn,const string &ln,const string &j,const string &rpo):emp(fn,ln,j),reportsto(rpo){}
	fink(const emp &e,const string &rpo):emp(e),reportsto(rpo){}
	fink(const fink &e):emp(e)
	{
		reportsto=e.reportsto;
	}
	virtual void speciality()const
	{
		cout<<"this is fink:"<<endl;
	}
	virtual void showAll()const
	{
		emp::showAll();
		cout<<"Reportsto:"<<reportsto<<endl;
	}
	virtual void setAll()
	{
		emp::setAll();
		cout<<"Enter the reportsto:";
		//int n;
		//getline(cin,reportsto);
		cin>>reportsto;
		eatLine();
		//eatLine();
	}

	virtual void Data(ofstream &fout){//标记
		fout<<Fink<<endl;
	};
	virtual void save(ofstream &fout)
	{
		emp::save(fout);
		fout<<reportsto<<endl;
	};//保存到文本
	virtual void getall(ifstream &fin)
	{
		emp::getall(fin);
		fin>>reportsto;
	}

};

class highfink:public manager,public fink
{
public:
	
	highfink(){};
	highfink(const string &fn,const string &ln,const string &j,const string &rpo,int ico)
		:emp(fn,ln,j),manager(fn,ln,j,ico),fink(fn,ln,j,rpo){}
	highfink(const emp &e,const string &rpo,int ico)
		:emp(e),manager(e,ico),fink(e,rpo){}//注意这个构造是怎么构造的
	highfink(const fink &f,int ico)
		:emp(f),fink(f),manager(f,ico){}//这样行不行呢?
		
	highfink(const manager &m,const string &rpo)
	:emp(m),manager(m),fink(m,rpo){}

	highfink(const highfink &h)
		:emp(h),manager(h),fink(h){}//这样可否??

	virtual void speciality()const//const必须与emp一致,否则会隐藏原来的,而不是重载
	{
		cout<<"This is highfink:"<<endl;
	}
	virtual void showAll()const//注意大小写不要写错了
	{
		//out<<"here is !"<<endl;
		emp::showAll();
		cout<<"Inchargeof:"<<InChargeOf()<<"   ";
		cout<<"Reportsto:"<<ReportsTo()<<endl;
		/*manager::showAll();
		fink::showAll();*/
	}
	virtual void setAll()
	{
		emp::setAll();
		cout<<"Enter the inchargeof:";
		int incharge;
		cin>>incharge;
		cin.get();//吃掉\n
		setI(incharge);
		cout<<"Enter the reportsto:";
		string res;
		cin>>res;
		setRe(res);
		/*
		manager::setAll();
		fink::setAll();*/
	}
	virtual void getall(ifstream &fin)
	{
		emp::getall(fin);
		
	}



	virtual void Data(ofstream &fout){//标记
		fout<<Highfink<<endl;
	};
	virtual void save(ofstream &fout){//要特殊处理
		emp::save(fout);
		//Data(fout);
		int n=InChargeOf();
		fout<<n<<endl;
		string str=ReportsTo();
		fout<<str<<endl;
	};//保存到文本


};


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值