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


 

一开始看到题目这么长,差点就不想写了,而且感觉会很麻烦,但是最后想想,要有始有终,还是耐着性子写了下来,果然很麻烦的说~~~

但是还是写了,这样,到时候有人来到这里就不会出现找不到题目的问题!

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;
		fin>>lname;
		fin>>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);
		int i;
		fin>>i;
		setI(i);
		string str;
		fin>>str;
		setRe(str);
	}



	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;
	};//保存到文本


};

main176.cpp

//没有按照他说的那个命令行参数,因为觉得不用打开多个文件

#include <iostream>
#include "ee.cpp"
#include <fstream>
#include <cstdlib>	//for exit
using namespace std;
static const int MAX=1;
//int main(int argc,char *argv[])
int main()
{

	emp *pc[MAX];
	ofstream fout;
	ifstream fin("data.txt");
	if(!fin.is_open())
	{
		cout<<"FIN fail"<<endl;
		return 0;
	}
	//show
	char s;
	emp *p;
	while(!fin.eof()&&fin.good()){
		fin>>s;
		if(s=='\n')
			break;//如果到了最后一个换行
		switch(s)
		{
			case '1':p=new employee;
				break;
			case '2':p=new manager;
				break;
			case '3':p=new fink;
				break;
			case '4':p=new highfink;
				break;
			default:;break;
		}
		p->getall(fin);
		p->showAll();
		cout<<endl<<endl;

	}
	fin.close();





	//先读取,再写入
	fout.open("data.txt",ios_base::app);//原来是二进制文件无法追加
										//||ios_base::binary);
	if(!fout.is_open())
	{
		cout<<"Open fail!"<<endl;
		eatLine();
		return 1;
	}
	char ch='0';
	int i=0;
	while(ch!='q'&&i<MAX)
	{
		cout<<"Please enter the choice (q to quit):"<<endl
			<<"1.employee        2.manager"<<endl
			<<"3.fink            4.highfink"<<endl;
		cin>>ch;
		switch(ch)
		{
			case '1':pc[i]=new employee;
				break;
			case '2':pc[i]=new manager;
				break;
			case '3':pc[i]=new fink;
				break;
			case '4':pc[i]=new highfink;
				break;
			default:;break;
		}
		//eatLine();
		cin.get();
		pc[i]->setAll();
		pc[i]->showAll();
		pc[i]->save(fout);
		fout.flush();
		i++;
	}


	fout.close();

	cin.get();
	return 0; 
}

 

——————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:https://www.royalchen.com/

author:royalchen

Email:royalchen@royalchen.com

———————————————————————————————————————————————————

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注