当前位置:网站首页>Multiple inheritance virtual base exercises

Multiple inheritance virtual base exercises

2022-04-23 14:06:00 JokerYourMemory

Title From C++ primer plus After-school exercises 14-5

Code is divided into :

emp.h: The statement 4 Classes , They are virtual base classes abstr_emp,employee Class inherits this base class ,manager Virtual inheritance abstr_emp,fink Virtual inheritance abstr_emp,highfink multiple inheritance manager and employee.

emp.cpp: Provide function definitions .

main.cpp: Verification code .

#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 {};// Once declared, provide the definition , In this case, the method of inlining is adopted .
};


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;}// Two Inchargeof Method , use protected type , Provide access to derived classes inchargeof The way 
	int & InChargeOf() { return inchargeof; }
public:
	manager();
	manager(const string& fn, const string& ln, const string& j,const int ico = 0);// Don't redefine the default values 
	manager(const abstr_emp&e,int ico);
	manager(const manager& m);// Note that the function definition does not enter an endless loop , Call yourself 
	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:
	// Various construction methods depend on the constructor of the base class . 
	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 class 
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 class 
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();// Writing like this will still cause the call to be employee Of showall To enter the dead cycle 
}
void employee::SetAll()
{
	abstr_emp::SetAll();
}

//manager class 
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) {}

// Can this be omitted ?
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 class   overseer 
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 class 
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 Whether it needs to be converted into a base class ?

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 :  Defines the entry point for the console application .
//

#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;
}


Results output :

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://yzsam.com/2022/04/202204231401090931.html