大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
using namespace std;
#include "emp.h"
int main()
{
employee em("Trip", "Harris", "Thumper");
cout << em << endl;
em.ShowAll();
manager ma("Amorphia", "Spindragon", "Nuancer", 5);
cout << ma << endl;
em.ShowAll();
fink fi("Matt", "Oggs", "Oiler", "Juno Barr");
cout << fi << endl;
fi.ShowAll();
highfink hf(ma, "Curly Kew");
hf.ShowAll();
cout << "Press a key for next phase:\n";
while (cin.get() != '\n')continue;
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();
return 0;
}
emp.h
#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<<(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 override;
virtual void SetAll() override;
};
class manager :virtual public abstr_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 ico = 0);
manager(const abstr_emp& e, int ico);
manager(const manager& m);
virtual void ShowAll()const override;
virtual void SetAll()override;
};
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& pro);
fink(const abstr_emp& e, const string& pro);
fink(const fink& e);
virtual void ShowAll()const override;
virtual void SetAll()override;
};
class highfink :public manager, public fink
{
public:
highfink();
highfink(const string& fn, const string& ln, const string& j, const string& pro, int ico);
highfink(const abstr_emp& e, const string& rpo, int ico);
highfink(const fink& f, int ico);
highfink(const manager& m, const string& rpo);
highfink(const highfink& h);
virtual void ShowAll()const override;
virtual void SetAll()override;
};
emp.cpp
#include "emp.h"
abstr_emp::abstr_emp():lname("none"),fname("none"),job("none")
{
}
abstr_emp::abstr_emp(const string& fn, const string& ln, const string& j)
{
lname = ln;
fname = fn;
job = j;
}
void abstr_emp::ShowAll()const
{
cout << "NAME: " << fname << "." << lname << endl;
cout << "JOB TITLE: " << job << endl;
}
void abstr_emp::SetAll()
{
cout << "Entenr the first name: ";
getline(cin, fname);
cout << "Entenr the last name: ";
getline(cin, lname);
cout << "Entenr the job title: ";
getline(cin, job);
}
ostream& operator<<(ostream&os, const abstr_emp& e)
{
os << "NAME: " << e.fname << "." << e.lname << endl;
os << "JOB TITLE: " << e.job << endl;
return os;
}
abstr_emp::~abstr_emp()
{
}
employee::employee():abstr_emp()
{
}
employee::employee(const string& fn, const string& ln, const string& j):abstr_emp(fn, ln, j)
{
}
void employee::ShowAll()const
{
abstr_emp::ShowAll();
}
void employee::SetAll()
{
abstr_emp::SetAll();
}
manager::manager():abstr_emp()
{
inchargeof=0;
}
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 = m.inchargeof;
}
void manager::ShowAll()const
{
abstr_emp::ShowAll();
cout << "IN CHARGE OF:" << inchargeof << endl;
}
void manager::SetAll()
{
abstr_emp::SetAll();
cout << "Enter the number of in charge:";
cin >> inchargeof;
while (cin.get() != '\n')continue;
}
fink::fink():abstr_emp()
{
}
fink::fink(const string& fn, const string& ln, const string& j, const string& pro):abstr_emp(fn,ln,j)
{
this->reportsto = pro;
}
fink::fink(const abstr_emp& e, const string& pro):abstr_emp(e)
{
this->reportsto = pro;
}
fink::fink(const fink& e):abstr_emp(e)
{
this->reportsto = e.reportsto;
}
void fink::ShowAll()const
{
abstr_emp::ShowAll();
cout << "REPORT TO:" << reportsto << endl;
}
void fink::SetAll()
{
abstr_emp::SetAll();
cout << "Enter the reports to whom: ";
getline(cin, reportsto);
}
highfink::highfink():abstr_emp(),manager(),fink()
{
}
highfink::highfink(const string& fn, const string& ln, const string& j, const string& pro, int ico):abstr_emp(fn,ln,j),manager(fn,ln,j,ico),fink(fn,ln,j,pro)
{
}
highfink::highfink(const abstr_emp& e, const string& pro, int ico):abstr_emp(e),manager(e,ico),fink(e,pro)
{
}
highfink::highfink(const fink& f, int ico):abstr_emp(f),fink(f),manager(f,ico)
{
}
highfink::highfink(const manager& m, const string& rpo):abstr_emp(m),manager(m),fink(m,rpo)
{
}
highfink::highfink(const highfink& h):abstr_emp(h),manager(h),fink(h)
{
}
void highfink::ShowAll()const
{
manager::ShowAll();
cout << "Reportsto: " << ReportsTo() << endl;
cout << endl;
}
void highfink::SetAll()
{
manager::SetAll();
cout << "Enter the reportsto:";
getline(cin, fink::ReportsTo());
}
运行结果:
考查点:
- 虚继承
- 抽象类
- 菱形继承
- 初始化列表
2024年9月11日15:53:51