charpter14_homework.cpp
#include<iostream>
#include<string>
#include<cstring>
#include"wine_1.h"
#include"wine_2.h"
#include"queuetp.h"
#include"workermi.h"
#include"person.h"
#include"emp.h"
using namespace std;
void p14_1(void);
void p14_3(void);
void p14_4(void);
void p14_5(void);
//主函数
int main()
{
//p14_1();
//p14_3();
//p14_4();
p14_5();
return 0;
}
//------------作业一/作业二----------
void p14_1(void)
{
cout<<"Enter name of wine: ";
char lab[50];
cin.getline(lab,50);
cout<<"Enter name of years: ";
int yrs;
cin>>yrs;
Wine holding(lab,yrs);
holding.GetBottles();
holding.show();
const int YRS=3;
int y[YRS]={1993,1995,1998};
int b[YRS]={48,60,72};
Wine more("AAA",YRS,y,b);
more.show();
cout<<"sum: "<<more.sum()<<endl;
cout<<"Bye!\n";
return ;
}
//------------作业三---------
void p14_3(void)
{
const int Max=3;
//创建一个存储Worker指针的空队列
Queue<Worker*> quew(Max);
int ct;
for(ct=0;ct<Max;ct++)
{
char choice;
cout<<"Enter the emplyee category:\n"
<<"w:waiter s:singer t:singgingwaiter q:quit1\n";
cin>>choice;
while(strchr("wstq",choice)==NULL)
{
cout<<"Please enter again:";
cin>>choice;
}
if(quew.isfull())
{
cout<<"Full!\n";
break;
}
if(choice=='q')
break;
Worker* temp;
switch(choice)
{
case 'w':temp=new Waiter;
quew.enqueue(temp);
break;
case 's':temp=new Singer;
quew.enqueue(temp);
break;
case 't':temp=new SingingWaiter;
quew.enqueue(temp);
break;
}
cin.get();
temp->Set();
}
int i=0;
for(i=0;i<ct;i++)
{
cout<<endl;
Worker* temp;
quew.dequeue(temp);
temp->Show();
delete temp;
}
}
//----------作业四------------
void p14_4(void)
{
const int SIZE=3;
//应用多态
Person* lolas[SIZE];
int ct;
for(ct=0;ct<SIZE;ct++)
{
char choice;
cout<<"Enter the person category: \n"
<<"g:gunslinger p: poker b: baddude "
<<" q: quit\n";
cin>>choice;
while(strchr("gpbq",choice)==NULL)
{
cout<<"Enter again!\n";
cin>>choice;
}
while(cin.get()!='\n')
continue;
if(choice=='q')
break;
string fname;
string lname;
cout<<"Enter the first name: ";
getline(cin,fname);
cout<<"Enter the last name: ";
getline(cin,lname);
int notch;
switch(choice)
{
case 'g':cout<<"Enter the notch: ";
cin>>notch;
lolas[ct]=new Gunslinger(fname,lname,notch);
break;
case 'p':lolas[ct]=new PokerPlayer(fname,lname);
break;
case 'b':
cout<<"Enter the notch: ";
cin>>notch;
lolas[ct]=new BadDude(fname,lname,notch);
break;
}
while(cin.get()!='\n')
continue;
}
cout<<endl;
cout<<"Here is your staff:\n";
int i;
for(int i=0;i<ct;i++)
{
cout<<endl;
lolas[i]->Show();
}
for(i=0;i<ct;i++)
delete lolas[i];
cout<<"Bye!\n";
}
//----------作业五-------------
void p14_5(void)
{
employee em("AAA","BBB","CCC");
cout<<em<<endl;
em.ShowAll();
manager ma("DDD","EEE","FFF",5);
cout<<ma<<endl;
ma.ShowAll();
fink fi("GGG","HHH","III","JJJ");
cout<<fi<<endl;
fi.ShowAll();
highfink hf(ma,"KKK");
hf.ShowAll();
cout<<"Press a key for next phase\n";
cin.get();
highfink hf2;
hf2.SetAll();
cout<<"using an abstr_emp*:\n";
abstr_emp* tri[4]={&em,&fi,&hf,&hf2};
for(int i=0;i<4;i++)
tri[i]->ShowAll();
}
wine_1.h
#ifndef WINE_1_H_
#define WINE_1_H_
#include<iostream>
#include<valarray>
#include<string>
#include<cstring>
using std::string;
using std::cout;
using std::endl;
using std::cin;
//类Pair的声明
template<class T1,class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
Pair(T1& aval,T2& bval):a(aval),b(bval){};
Pair(){};//默认构造函数
T1& Year();
T2& Bottle(){return b;};
T1 Year()const{return a;};
T2 Bottle()const{return b;};
};
//类Pair的方法定义
template<class T1,class T2>
T1& Pair<T1,T2>::Year()
{
return a;
}
//类Wine,包含关系,has-a
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;
class Wine
{
private:
string name;
PairArray infor;
int year;
public:
//默认构造函数
Wine();
Wine(const char* l,int y,const int yr[],const int bot[]);
Wine(const char* l,int y);
void GetBottles();//提示用户输入年份和瓶数
string& Label(){return name;};//返回酒的名称
int sum();//返回瓶数总和
void show();
};
//类Wine,方法定义
//默认构造函数
Wine::Wine():name(),year(0),infor()
{
}
//构造函数,成员初始化列表
Wine::Wine(const char* l,int y,const int yr[],const int bot[]):name(l),year(y)
{
infor=PairArray(ArrayInt(y),ArrayInt(y));
for(int i=0;i<y;i++)
{
infor.Year()[i]=yr[i];
infor.Bottle()[i]=bot[i];
}
}
Wine::Wine(const char* l,int y):name(l),year(y)
{
infor=PairArray(ArrayInt(y),ArrayInt(y));
}
void Wine::GetBottles()//提示用户输入年份和瓶数
{
for(int i=0;i<year;i++)
{
cout<<"Enter year: ";
cin>>infor.Year()[i];
cout<<"Enter the bottles for that year: ";
cin>>infor.Bottle()[i];
}
}
void Wine::show()
{
cout<<"Wine: "<<name<<endl;
cout<<"\tYear\tBottles\n";
for(int i=0;i<year;i++)
cout<<"\t"<<infor.Year()[i]<<"\t"<<infor.Bottle()[i]<<endl;
}
int Wine::sum()//返回瓶数总和
{
return infor.Bottle().sum();
}
#endif
Wine_2.h
#ifndef WINE_2_H_
#define WINE_2_H_
#include<iostream>
#include<valarray>
#include<string>
#include<cstring>
using std::string;
using std::cout;
using std::endl;
using std::cin;
//类Pair的声明
template<class T1,class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
Pair(T1& aval,T2& bval):a(aval),b(bval){};
Pair(){};//默认构造函数
T1& Year();
T2& Bottle(){return b;};
T1 Year()const{return a;};
T2 Bottle()const{return b;};
virtual~Pair(){};
};
//类Pair的方法定义
template<class T1,class T2>
T1& Pair<T1,T2>::Year()
{
return a;
}
//类Wine,私有继承,has-a
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt,ArrayInt> PairArray;
class Wine:private PairArray,private string
{
private:
int year;
public:
//默认构造函数
Wine();
Wine(const char* l,int y,const int yr[],const int bot[]);
Wine(const char* l,int y);
void GetBottles();//提示用户输入年份和瓶数
const string& Label(){return (const string& )(*this);};//返回酒的名称
int sum();//返回瓶数总和
void show();
};
//类Wine,方法定义实现
Wine::Wine(const char* l,int y,const int yr[],
const int bot[]):string(l),year(y),PairArray(ArrayInt(y),ArrayInt(y))
{
for(int i=0;i<y;i++)
{
PairArray::Year()[i]=yr[i];
PairArray::Bottle()[i]=bot[i];
}
}
Wine::Wine():string("none"),year(0),PairArray(ArrayInt(),ArrayInt())
{
}
Wine::Wine(const char* l,int y):string(l),year(y),PairArray(ArrayInt(y),ArrayInt(y))
{
}
void Wine::GetBottles()//提示用户输入年份和瓶数
{
for(int i=0;i<year;i++)
{
cout<<"Enter year: ";
cin>>PairArray::Year()[i];
cout<<"Enter the bottles for that year: ";
cin>>PairArray::Bottle()[i];
}
}
int Wine::sum()//返回瓶数总和
{
return PairArray::Bottle().sum();
}
void Wine::show()
{
cout<<"Wine: "<<(const string&)(*this)<<endl;
cout<<"\tYear\tBottles\n";
for(int i=0;i<year;i++)
cout<<"\t"<<PairArray::Year()[i]<<"\t"<<PairArray::Bottle()[i]<<endl;
}
#endif
queuetp.h
#ifndef QUEUETP_H_
#define QUEUETP_H_
//模板类声明
template<typename T>
class Queue
{
private:
//节点定义
struct Node{T item;struct Node* next;};
enum{Q_SIZE=10};
//成员
Node* front;
Node* rear;
int items;
const int qsize;//队列最大数目,不是静态成员
//伪私有方法
Queue(const Queue& q):qsize(0){};
Queue& operator=(const Queue& q){return *this;};
public:
Queue(int qs=Q_SIZE);
~Queue();
bool isempty()const{return items==0;};
bool isfull()const{return items==qsize;};
int queuecount()const{return items;};
bool enqueue(const T& item);
bool dequeue(T& item);
};
//类模板方法定义
template<typename T>
Queue<T>::Queue(int qs=Q_SIZE):qsize(qs)
{
front=rear=nullptr;
items=0;
}
template<typename T>
Queue<T>::~Queue()
{
Node* temp;
while(front!=nullptr)
{
temp=front;
front=front->next;
delete temp;
}
}
template<typename T>
bool Queue<T>::enqueue(const T& item)
{
//判断是否满
if(isfull())
return false;
//new一个新内存
Node* add=new Node;
add->item=item;
add->next=nullptr;
items++;
//处理连接问题
if(front==nullptr)
front=add;
else
rear->next=add;
rear=add;
return true;
}
template<typename T>
bool Queue<T>::dequeue(T& item)
{
//判断是否为空
if(isempty())
return false;
item=front->item;
items--;
Node* temp=front;
front=front->next;
delete temp;
if(items==0)
rear=nullptr;
return true;
}
#endif
workermi.h
#ifndef WORKERMI_H_
#define WORKERMI_H_
#include<string>
using std::string;
class Worker//抽象基类,至少有一个纯虚函数
{
private:
std::string fullname;//包含类成员
long id;
protected://给派生类用,模块化
virtual void Data()const;
virtual void Get();
public:
//此处要使用成员初始化构造函数
Worker():fullname("none"),id(0L){};
Worker(const std::string& s,long n):fullname(s),id(n){};//包含使用变量名
virtual ~Worker()=0;//纯虚函数
virtual void Set()=0;
virtual void Show()const=0;
};
class Waiter:virtual public Worker
{
private:
int panache;
protected:
void Data()const;
void Get();
public:
Waiter():Worker(),panache(0){};
Waiter(const std::string& s,long n,int p=0):Worker(s,n),panache(p){};
Waiter(const Worker& w,int p=0):Worker(w),panache(p){};
void Set();
void Show()const;
};
class Singer:virtual public Worker
{
protected:
enum{other,alto,contralto,soprano,bass,baritone,tensor};
enum{Vtypes=7};
void Data()const;
void Get();
private:
//静态成员变量在头文件声明,方法文件中初始化,所有类成员共享一个内存
static char* pv[Vtypes];
int voice;
public:
Singer():Worker(),voice(0){};
Singer(const std::string& s,long n,int v=other):Worker(s,n),voice(v){};
Singer(const Worker& w,int v=other):Worker(w),voice(v){};
void Set();
void Show()const;
};
class SingingWaiter:public Singer,public Waiter
{
protected:
void Data()const;
void Get();
public:
//SingingWaiter(){};
SingingWaiter():Worker(),Singer(),Waiter(){};
SingingWaiter(const std::string& s,long n,int v=other,int p=0):Worker(s,n),Waiter(s,n,p),Singer(s,n,v){};
SingingWaiter(const Worker& w,int v=other,int p=0):Worker(w),Waiter(w,p),Singer(w,v){};
SingingWaiter(const Waiter& w,int v=other):Worker(w),Waiter(w),Singer(w,v){};
SingingWaiter(const Singer& w,int p=0):Worker(w),Waiter(w,p),Singer(w){};
void Set();
void Show()const;
};
#endif
workermi.cpp
#include<iostream>
#include<string>
#include"workermi.h"
using std::cout;
using std::string;
using std::endl;
using std::getline;
using std::cin;
//抽象类,Worker
Worker::~Worker(){};//析构函数
void Worker::Data()const
{
cout<<"fullname: "<<fullname<<endl;
cout<<"id: "<<id<<endl;
}
void Worker::Get()
{
getline(cin,fullname);
cout<<"Enter the id: ";
cin>>id;
while(cin.get()!='\n')
continue;
}
//派生类 Waiter
void Waiter::Data()const//模块化
{
cout<<"panache: "<<panache<<endl;
}
void Waiter::Get()
{
cout<<"Enter the panache: ";
cin>>panache;
while(cin.get()!='\n')
continue;
}
void Waiter::Set()
{
cout<<"Enter the waiter's name: ";
Worker::Get();
Get();
}
void Waiter::Show()const
{
cout<<"Category:waiter: \n";
Worker::Data();
Data();
}
//派生类 Singer
//静态成员变量在头文件声明,方法文件中初始化,所有类成员共享一个内存
char* Singer::pv[Singer::Vtypes]={"other","alto","contralto","soprano","bass","baritone","tensor"};
//方法定义
void Singer::Data()const
{
cout<<"Vocal range: "<<pv[voice]<<endl;
}
void Singer::Get()
{
cout<<"Enter singer's vocal range:\n";
int i;
for(i=0;i<Vtypes;i++)
{
cout<<i<<" : "<<pv[i]<<endl;
if(i%4==3)
cout<<endl;
}
if(i%4!=0)
cout<<endl;
cin>>voice;
while(cin.get()!='\n')
continue;
}
void Singer::Set()
{
cout<<"Enter the singer's name: ";
Worker::Get();
Get();
}
void Singer::Show()const
{
cout<<"Category: singer\n";
Worker::Data();
Data();
}
//派生类 Singingwaiter
void SingingWaiter::Data()const
{
Singer::Data();
Waiter::Data();
}
void SingingWaiter::Get()
{
Singer::Get();
Waiter::Get();
}
void SingingWaiter::Set()
{
cout<<"Enter the Singingwaiter's name: ";
Worker::Get();
Get();
}
void SingingWaiter::Show()const
{
cout<<"Category: singsing waiter\n";
Worker::Data();
Data();
}
person.h
#ifndef PERSON_H_
#define PERSON_H_
#include<string>
using std::string;
//基类 Person,抽象基类,包含一个纯虚函数
class Person
{
private:
string first_name;
string last_name;
protected:
virtual void Data()const;
public:
//构造函数,默认构造
Person(const string& fn="none",const string& ln="none"):first_name(fn),last_name(ln){};
virtual void Show()const=0;
virtual double Draw()const=0;
//析构,纯虚函数
virtual ~Person(){};
};
//派生类Gunslinger,虚基类派生,is-a关系,公有继承
class Gunslinger:virtual public Person
{
private:
int notch;//刻痕数
protected:
void Data()const;//模块化
public:
Gunslinger(const string& fn="none",const string& ln="none",
int n=0):Person(fn,ln),notch(n){};
void Show()const;
double Draw()const;
};
//派生类 PokerPlayer
class PokerPlayer:virtual public Person
{
protected:
void Data()const;//模块化
public:
PokerPlayer(const string& fn="none",const string& ln="none"):Person(fn,ln){};
void Show()const;
double Draw()const;
};
//派生类 BadDude
class BadDude:public Gunslinger,public PokerPlayer
{
protected:
void Data()const{};//模块化
public:
BadDude(const string& fn="none",const string& ln="none",int n=0):Person(fn,ln),
Gunslinger(fn,ln,n),PokerPlayer(fn,ln){};
double Gdraw()const;
int Cdraw()const;
void Show()const;
double Draw()const;
};
#endif
person.cpp
#include<iostream>
#include<ctime>
#include<cstdlib>
#include"person.h"
using namespace std;
//基类 Person方法实现
void Person::Data()const
{
cout<<"first_name: "<<first_name<<endl;
cout<<"last_name: "<<last_name<<endl;
}
//派生类Gunslinger,方法实现
void Gunslinger::Data()const//模块化
{
cout<<"notches: "<<notch<<endl;
}
void Gunslinger::Show()const
{
Person::Data();
Data();
}
double Gunslinger::Draw()const
{
srand(time(0));//随机数种子
return (double)(rand()%10)/10;
}
//派生类 PokerPlayer,方法实现
double PokerPlayer::Draw()const
{
srand(time(0));//随机数种子
return (double)(rand()%52);
}
void PokerPlayer::Show()const
{
Person::Data();
Data();
}
void PokerPlayer::Data()const//模块化
{
cout<<"poker: "<<Draw()<<endl;
}
//派生类 BadDude
double BadDude::Gdraw()const
{
return Gunslinger::Draw();
}
int BadDude::Cdraw()const
{
return (int)PokerPlayer::Draw();
}
void BadDude::Show()const
{
Person::Data();
Gunslinger::Data();
PokerPlayer::Data();
}
double BadDude::Draw()const
{
return Gunslinger::Draw();
}
emp.h
#ifndef EMP_H_
#define EMP_H_
#include<string>
#include<iostream>
using std::string;
using std::ostream;
class abstr_emp
{
private:
string fname;
string lname;
string job;
public:
abstr_emp(){fname="none";lname="none";job="none";};
abstr_emp(const string& fn,const string& ln,const string& j):fname(fn),lname(ln),job(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():abstr_emp(){};
employee(const string& fn,const string& ln,const string& j):abstr_emp(fn,ln,j){};
virtual void ShowAll()const;
virtual void SetAll();
};
class manager:virtual public abstr_emp
{
private:
int inchargeof;
protected:
int InChargeOf()const{return inchargeof;};
int & InChargeOf(){return inchargeof;};
public:
manager():abstr_emp(),inchargeof(0){};
manager(const string& fn,const string& ln,const string& j,
int ico=0):abstr_emp(fn,ln,j),inchargeof(ico){};
manager(const abstr_emp& e,int ico):abstr_emp(e),inchargeof(ico){};
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():abstr_emp(),reportsto("none"){};
fink(const string& fn,const string& ln,const string& j,
const string& rpo):abstr_emp(fn,ln,j),reportsto(rpo){};
fink(const abstr_emp& e,const string& rpo):abstr_emp(e),reportsto(rpo){};
virtual void ShowAll()const;
virtual void SetAll();
};
class highfink:public manager,public fink
{
public:
highfink():abstr_emp(),manager(),fink(){};
highfink(const string& fn,const string& ln,const string& j,
const string& rpo,int ico=0):abstr_emp(fn,ln,j),manager(fn,ln,j,ico),fink(fn,ln,j,rpo){};
highfink(const abstr_emp& e,const string& rpo,int ico):abstr_emp(e),manager(e,ico),fink(e,rpo){};
highfink(const fink& f,int ico):abstr_emp(f),manager(f,ico),fink(f){};
highfink(const manager& f,const string& rpo):abstr_emp(f),manager(f),fink(f,rpo){};
virtual void ShowAll()const;
virtual void SetAll();
};
#endif
emp.cpp
#include<iostream>
#include<string>
#include"emp.h"
using namespace std;
//基类 abstr_emp
void abstr_emp::ShowAll()const
{
cout<<"first name: "<<fname<<endl;
cout<<"last name: "<<lname<<endl;
cout<<"job: "<<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: ";
getline(cin,job);
}
ostream& operator<<(ostream& os,const abstr_emp& e)
{
os<<e.fname<<" , "<<e.lname<<" , "<<e.job<<endl;
return os;
}
abstr_emp::~abstr_emp(){};//析构函数
//派生类 employee
void employee::ShowAll()const
{
abstr_emp::ShowAll();
}
void employee::SetAll()
{
abstr_emp::SetAll();
}
//派生类 manager
void manager::ShowAll()const
{
abstr_emp::ShowAll();
cout<<"inchargeof: "<<inchargeof<<endl;
}
void manager::SetAll()
{
abstr_emp::SetAll();
cout<<"Enter the inchargeof: ";
cin>>inchargeof;
while(cin.get()!='\n')
continue;
}
//派生类 fink
void fink::ShowAll()const
{
abstr_emp::ShowAll();
cout<<"reportsto: "<<reportsto<<endl;
}
void fink::SetAll()
{
abstr_emp::SetAll();
cout<<"Enter the inchargeof: ";
getline(cin,reportsto);
}
//派生类 highfink
void highfink::ShowAll()const
{
abstr_emp::ShowAll();
cout<<"inchargeof: "<<manager::InChargeOf()<<endl;
cout<<"reportsto: "<<fink::ReportsTo()<<endl;
}
void highfink::SetAll()
{
abstr_emp::SetAll();
cout<<"Enter the inchargeof: ";
cin>>manager::InChargeOf();
while(cin.get()!='\n')
continue;
cout<<"Enter the inchargeof: ";
getline(cin,fink::ReportsTo());
}
如有错误,欢迎改正!
这段代码展示了C++中多态的运用,包括葡萄酒库存管理、员工队列管理以及不同角色的员工信息处理。程序包含了葡萄酒类`Wine`的两个实现,使用了模板类`Queue`进行员工`Worker`指针的存储,以及抽象基类`Person`和其派生类如`Gunslinger`、`PokerPlayer`、`BadDude`的定义,以及员工类`Employee`和其子类`Manager`、`Fink`、`HighFink`的实现。
3691

被折叠的 条评论
为什么被折叠?



