C++学习-----------虚基类的MI

 
/***************************************************************************
注意,该类层次结构使用了带虚基类的MI,所以要牢记这种情况下用于构造化列表的特
规则。还需要注意的是,有些方法被声明为保护的。这可以简单化一些highfink方法的
代码(例如,如果highfink::ShowAll()只是调用fink::ShowAll()和manager::ShowAll()
,则它将调用abstr_emp::ShowAll()两次)。请提供类方法的实现,并在一个程序中对这些
类方法进行测试。下面是一个小型测试程序:
***************************************************************************/




/***************************************************************************
useemp.cpp

功能:提供测试程序
备注:无

****************************************************************************/

#include<iostream>
using namespace std ;
#include"emp.h"



int main(void)
{
	employee em("Trip" ,"Harris","Thumper") ;

	cout << em << endl ;
	em.ShowAll() ;

	cout << endl ;

	manager ma("Amorphia" , "Spindragon" , "Nuancer" ,5 ) ;
	cout << ma << endl ;
	ma.ShowAll () ;

	cout << endl ;

	fink fi("Matt" ,"Oggs" ,"Oiler","Juno Barr" ) ;
	cout << fi << endl ;
	fi.ShowAll () ;

	cout << endl ;

	highfink hf(ma,"Curly Kew") ;
	hf.ShowAll () ;
	
	cout << "Press a key for nex phase : \n" ;

	cin.get() ;

	while((cin.get()) != '\n')
		continue ;

	highfink hf2 ;
	hf2.SetAll() ;

	cout << "Using an abstr_emp * pointer : \n" ;
	abstr_emp *tri[4] = {&em , &fi , &hf ,&hf2} ;

	cout << endl ;
	
	for(int i = 0 ; i < 4 ; i++)
	{
		tri[i]->ShowAll() ;
		cout << endl ;
	}


	return 0 ;
}










/*********************************************************
emp.h

  功能:提供一些类声明

  备注:
**********************************************************/



#ifndef EMP_H_
#define EMP_H_

#include<iostream>
#include<string>



//class abstr_emp


class abstr_emp
{
private :
	std::string fname ;
	std::string lname ;
	std::string job ;

public :
	abstr_emp() ;
	abstr_emp(const std::string &fn , const std::string &ln,const std::string &j) ;
	virtual void ShowAll() const ;
	virtual void SetAll() ;
	friend std::ostream & operator<<(std::ostream &os, const abstr_emp &e) ;
	virtual ~abstr_emp() = 0 {}
} ;




//class employee

class employee : public abstr_emp
{
public :
	employee() ;
	employee(const std::string &fn , const std::string &ln ,const std::string &j) ;
	virtual void ShowAll() const ;
	virtual void SetAll() ;
} ;



//class manager

class manager: virtual public abstr_emp
{
private :
	int inchargeof ;

protected :
	int InChargeOf() const {return inchargeof ;}
	int & InChargeOf() {return inchargeof ;} 

public :
	manager() ;
	manager(const std::string &fn ,const std::string &ln ,const std::string &j ,int ico = 0 ) ;
	manager(const abstr_emp &e ,int ico) ;
	manager(const manager &m) ;
	virtual void ShowAll() const ;
	virtual void SetAll() ;
} ;


//class fink

class fink:virtual public abstr_emp 
{
private :
	std::string reportsto ;

protected : 
	const std::string  ReportsTo() const {return reportsto ;} 
	std::string  &ReportsTo() {return reportsto ;}

public :
	fink() ;
	fink(const std::string &fn ,const std::string &ln,const std::string &j ,const std::string &rpo) ;
	fink(const abstr_emp &e,const std::string &rpo) ;
	fink(const fink &e ) ;
	virtual void ShowAll() const ;
	virtual void SetAll() ;

} ;


//class highfink

class highfink:public manager ,public fink 
{
public :
	highfink() ;
	highfink(const std::string &fn ,const std::string &ln,const std::string &j,const std::string &rpo,int ico) ;
	highfink(const abstr_emp &e , const std::string &rpo ,int ico) ;
	highfink(const fink &f ,int ico) ;
	highfink(const manager &m ,const std::string &rpo) ;
	highfink(const highfink &h) ;
	virtual void ShowAll() const ;
	virtual void SetAll() ;
} ;






#endif











/**********************************************************
emp.cpp

  功能:提供类方法的实现
  备注:带虚基类的MI,构造函数的规则有变(包括复制构造函数)
		而且在Show()之类的方法,思路应该要转变,否则会调用
		两次。
**********************************************************/



#include"emp.h"

using namespace std ;




//class abstr_emp methods 

ostream & operator<<(ostream &os, const abstr_emp &e)
{	
	e.ShowAll () ;

	return os ;
}


void abstr_emp::SetAll () 
{
	cout << "Enter the first name : "  ;
	getline(cin,fname) ;

	cout << "Enter the last name : " ;
	getline(cin,lname) ;

	cout << "Enter the job : " ;
	getline(cin,job) ;

	while((cin.get()) != '\n')
		continue ;
} 



void abstr_emp::ShowAll () const 
{
	cout << fname << " , " << lname << endl ;
	cout <<	"Job : " << job << endl ;
}





abstr_emp::abstr_emp(const std::string &fn , const std::string &ln,const std::string &j):fname(fn) ,lname(ln),job(j) 
{
}



abstr_emp::abstr_emp()
{
}





//class  employee methods


void employee::SetAll()
{
	abstr_emp::SetAll () ;
}


void employee::ShowAll () const 
{	
	abstr_emp::ShowAll () ;
}


employee::employee(const std::string &fn , const std::string &ln ,const std::string &j):abstr_emp(fn,ln,j) 
{
	
}


employee::employee():abstr_emp()
{
}



 
//class  manager methods 



void manager::SetAll ()
{
	abstr_emp::SetAll () ;
	
	cout << "Enter the inchargeof : " ;
	cin >> inchargeof ;
}


void manager::ShowAll () const 
{
	abstr_emp::ShowAll () ;

	cout << "Inchargeof : " << inchargeof << endl ; 
}


manager::manager(const manager &m):abstr_emp(m) 
{
	inchargeof = m.inchargeof ;
}


manager::manager(const abstr_emp &e ,int ico):abstr_emp(e)
{	
	inchargeof = ico ;
}



manager::manager(const std::string &fn ,const std::string &ln ,const std::string &j ,int ico ):abstr_emp(fn,ln,j)
{
	inchargeof = ico ;
}

manager::manager():abstr_emp()
{

}



//class fink method

void fink::SetAll()
{
	abstr_emp::SetAll () ;
	cout << "Enter the reportsto : " ;
	cin >> reportsto ;
}



void fink::ShowAll() const 
{
	abstr_emp::ShowAll () ;
	cout << "reportsto : " << reportsto << endl ;
}




fink::fink(const fink &e):abstr_emp(e)
{
	reportsto = e.reportsto ;
}



fink::fink(const abstr_emp &e,const std::string &rpo):abstr_emp(e),reportsto(rpo)
{
}




fink::fink(const std::string &fn ,const std::string &ln,const std::string &j ,const std::string &rpo):abstr_emp(fn,ln,j),reportsto(rpo)
{	
}



fink::fink():abstr_emp()
{
}


//class highfink methods

void highfink::SetAll ()
{
	abstr_emp::SetAll () ;
	
	cout << "Enter the inchargeof : " ;
	cin >> InChargeOf() ;

	cout << "Enter the reportsto : " ;
	cin >> ReportsTo() ;
}


void highfink::ShowAll() const 
{
	abstr_emp::ShowAll () ;

	cout << "InChargeOf : " << InChargeOf() << endl ;
	cout << "ReportsTo : " << ReportsTo() << endl ;
}


highfink::highfink(const highfink &h):abstr_emp(h),manager(h),fink(h)
{
}


highfink::highfink(const manager &m ,const std::string &rpo):abstr_emp(m),manager(m),fink(m,rpo)
{
}


highfink::highfink(const fink &f ,int ico):abstr_emp(f),fink(f),manager(f,ico)
{
}


highfink::highfink(const abstr_emp &e , const std::string &rpo ,int ico):abstr_emp(e),manager(e,ico),fink(e,rpo)
{

}



highfink::highfink(const std::string &fn ,const std::string &ln,const std::string &j,const std::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()

{
}

你可能感兴趣的:(C++学习-----------虚基类的MI)