大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
#include <fstream>
#include "emp.h"
using namespace std;
const int MAX = 4;
enum ClassType{
Employee,Manager,Fink,HighFink};
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 << fi << endl;
ma.ShowAll();
highfink hf(ma, "Curly Kew");
hf.ShowAll();
cout << "Press a key for next phase: \n";
cin.get();
ofstream fout("EMP.TXT");
if (!fout.is_open()) {
exit(EXIT_FAILURE); }
cout << "Using an abstr_emp *pointer to write file." << endl;
abstr_emp* tri[MAX] = {
&em,&ma,&fi,&hf };
for (int i = 0; i < MAX; i++)
{
if(typeid(*tri[i])==typeid(employee))
{
fout << Employee << endl;
tri[i]->writeall(fout);
}
else if (typeid(*tri[i]) == typeid(fink))
{
fout << Fink << endl;
tri[i]->writeall(fout);
}
else if (typeid(*tri[i]) == typeid(manager))
{
fout << Manager << endl;
tri[i]->writeall(fout);
}
else if (typeid(*tri[i]) == typeid(highfink))
{
fout << HighFink << endl;
tri[i]->writeall(fout);
}
else
{
fout << -1 << endl;
tri[i]->writeall(fout);
}
}
fout.close();
abstr_emp* pc[MAX];
int classtype;
int i = 0;
ifstream fin("EMP.TXT");
if (!fin.is_open()) {
exit(EXIT_FAILURE); }
while (fin>>classtype)
{
fin.get();
switch (classtype)
{
case Employee:
pc[i] = new employee;
pc[i++]->getall(fin);
break;
case Manager:
pc[i] = new manager;
pc[i++]->getall(fin);
break;
case Fink:
pc[i] = new fink;
pc[i++]->getall(fin);
break;
case HighFink:
pc[i] = new highfink;
pc[i++]->getall(fin);
break;
default:
break;
}
}
for (i = 0; i < MAX; i++)pc[i]->ShowAll();
fin.close();
return 0;
}
emp.h
#pragma once
#include <iostream>
#include <string>
#include <fstream>
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;
virtual void writeall(ofstream& fout)const;
virtual void getall(ifstream& fin);
};
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;
virtual void writeall(ofstream& fout)const override;
virtual void getall(ifstream& fin)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;
virtual void writeall(ofstream& fout)const override;
virtual void getall(ifstream& fin)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&rpo);
fink(const fink& e);
virtual void ShowAll()const override;
virtual void SetAll()override;
virtual void writeall(ofstream& fout)const override;
virtual void getall(ifstream& fin)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& pro, 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;
virtual void writeall(ofstream& fout)const override;
virtual void getall(ifstream& fin)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) :fname(fn), lname(ln), job(j)
{
}
void abstr_emp::ShowAll()const
{
cout << "NAME: " << fname << "." << lname << endl;
cout << "JOB TITLE: " << job << endl;
}
void abstr_emp::SetAll()
{
cout << "Enter the first name: ";
getline(cin, fname);
cout << "Enter the last name: ";
getline(cin, lname);
cout << "Enter 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;
}
void abstr_emp::writeall(ofstream& fout)const
{
fout << fname << endl;
fout << lname << endl;
fout << job << endl;
}
void abstr_emp::getall(ifstream& fin)
{
getline(fin, fname);
getline(fin, lname);
getline(fin, job);
}
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();
}
void employee::writeall(ofstream& fout)const
{
abstr_emp::writeall(fout);
}
void employee::getall(ifstream& fin)
{
abstr_emp::getall(fin);
}
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;
}
}
void manager::writeall(ofstream& fout)const
{
abstr_emp::writeall(fout);
fout << inchargeof << endl;
}
void manager::getall(ifstream& fin)
{
abstr_emp::getall(fin);
fin >> inchargeof;
fin.get();
}
fink::fink():abstr_emp(),reportsto("none")
{
}
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 << "REPORT TO: " << reportsto << endl;
}
void fink::SetAll()
{
abstr_emp::SetAll();
cout << "Enter the reports to whom: ";
getline(cin, reportsto);
}
void fink::writeall(ofstream& fout)const
{
abstr_emp::writeall(fout);
fout << reportsto << endl;
}
void fink::getall(ifstream& fin)
{
abstr_emp::getall(fin);
getline(fin, reportsto);
}
highfink::highfink():abstr_emp(),manager(),fink()
{
}
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(const abstr_emp& e, const string& rpo, int ico) :abstr_emp(e), manager(e, ico), fink(e, rpo)
{
}
highfink::highfink(const fink& f, int ico) :abstr_emp(f), manager(f, ico), fink(f)
{
}
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());
}
void highfink::writeall(ofstream& fout)const
{
manager::writeall(fout);
fout << fink::ReportsTo() << endl;
}
void highfink::getall(ifstream& fin)
{
manager::getall(fin);
getline(fin, fink::ReportsTo());
}
运行结果:
2024年9月22日21:37:56