C++PrimerPlus第六版第十四章课后习题部分答案.docx
《C++PrimerPlus第六版第十四章课后习题部分答案.docx》由会员分享,可在线阅读,更多相关《C++PrimerPlus第六版第十四章课后习题部分答案.docx(23页珍藏版)》请在冰豆网上搜索。
![C++PrimerPlus第六版第十四章课后习题部分答案.docx](https://file1.bdocx.com/fileroot1/2023-1/4/f308f0e4-7c80-4f81-9483-652519b9b6a4/f308f0e4-7c80-4f81-9483-652519b9b6a41.gif)
C++PrimerPlus第六版第十四章课后习题部分答案
1.
Wine.h
#ifndefWINE_H_
#defineWINE_H_
#include
#include
#include
usingnamespacestd;
template
classPair
{
private:
T1years;
T2bottles;
public:
Pair(){}
Pair(constT1&t1,constT2&t2):
years(t1),bottles(t2){};
intSum()const;
voidshow(inty)const;
voidSet(constT1&t1,constT2&t2);
};
template
intPair:
:
Sum()const
{
returnbottles.sum();
}
template
voidPair:
:
show(inty)const
{
for(inti=0;icout<<"\t\t"<}
template
voidPair:
:
Set(constT1&t1,constT2&t2)
{
years=t1;
bottles=t2;
}
typedefvalarrayArrayInt;
typedefPairPairArray;
classWine
{
private:
std:
:
stringwinename;
intyear;
PairArrayout;
public:
Wine(){}
Wine(constchar*l,inty,constintyr[],constintbot[]);
Wine(constchar*l,inty);
voidGetBottle();
std:
:
string&Label();
intsum()const;
voidshow()const;
};
#endif
Wine.cpp
#include"wine.h"
Wine:
:
Wine(constchar*l,inty,constintyr[],constintbot[])
{
winename=l;
year=y;
out.Set(ArrayInt(yr,year),ArrayInt(bot,year));
}
Wine:
:
Wine(constchar*l,inty)
{
winename=l;
year=y;
}
voidWine:
:
GetBottle()
{
ArrayIntyr(year),bt(year);
for(inti=0;i{
cout<<"Enteryear:
";
cin>>yr[i];
cout<<"Enterbottles:
";
cin>>bt[i];
}
out.Set(yr,bt);
}
intWine:
:
sum()const
{
returnout.Sum();
}
std:
:
string&Wine:
:
Label()
{
returnwinename;
}
voidWine:
:
show()const
{
cout<<"Wine:
"<cout<<"\t\t"<<"Year"<<"\t"<<"Bottle\n";
out.show(year);
}
Main.cpp
#include
#include"wine.h"
intmain(void)
{
cout<<"Enternameofwine:
";
charlab[50];
cin.getline(lab,50);
cout<<"Enternumberofyears:
";
intyrs;
cin>>yrs;
Wineholding(lab,yrs);
holding.GetBottle();
holding.show();
constintYRS=3;
inty[YRS]={1993,1995,1998};
intb[YRS]={48,60,72};
Winemore("GushingGrapeRED",YRS,y,b);
more.show();
cout<<"Totalbottlesfor"<"<system("Pause");
return0;
}
2.
Wine.h
#ifndefWINE_H_
#defineWINE_H_
#include
#include
#include
usingnamespacestd;
template
classPair
{
private:
T1years;
T2bottles;
public:
Pair(){}
Pair(constT1&t1,constT2&t2):
years(t1),bottles(t2){};
intSum()const;
voidshow(inty)const;
voidSet(constT1&t1,constT2&t2);
};
template
intPair:
:
Sum()const
{
returnbottles.sum();
}
template
voidPair:
:
show(inty)const
{
for(inti=0;icout<<"\t\t"<}
template
voidPair:
:
Set(constT1&t1,constT2&t2)
{
years=t1;
bottles=t2;
}
typedefvalarrayArrayInt;
typedefPairPairArray;
classWine:
privatestring,privatePairArray
{
private:
std:
:
stringwinename;
intyear;
PairArrayout;
public:
Wine(){}
Wine(constchar*l,inty,constintyr[],constintbot[]);
Wine(constchar*l,inty);
voidGetBottle();
std:
:
string&Label();
intsum()const;
voidshow()const;
};
#endif
Wine.cpp
#include"wine.h"
Wine:
:
Wine(constchar*l,inty,constintyr[],constintbot[])
{
winename=l;
year=y;
out.Set(ArrayInt(yr,year),ArrayInt(bot,year));
}
Wine:
:
Wine(constchar*l,inty)
{
winename=l;
year=y;
}
voidWine:
:
GetBottle()
{
ArrayIntyr(year),bt(year);
for(inti=0;i{
cout<<"Enteryear:
";
cin>>yr[i];
cout<<"Enterbottles:
";
cin>>bt[i];
}
out.Set(yr,bt);
}
intWine:
:
sum()const
{
returnout.Sum();
}
std:
:
string&Wine:
:
Label()
{
return(string&)*this;
}
voidWine:
:
show()const
{
cout<<"Wine:
"<<(string&)*this<cout<<"\t\t"<<"Year"<<"\t"<<"Bottle\n";
out.show(year);
}
Main.cpp
#include
#include"wine.h"
intmain(void)
{
cout<<"Enternameofwine:
";
charlab[50];
cin.getline(lab,50);
cout<<"Enternumberofyears:
";
intyrs;
cin>>yrs;
Wineholding(lab,yrs);
holding.GetBottle();
holding.show();
constintYRS=3;
inty[YRS]={1993,1995,1998};
intb[YRS]={48,60,72};
Winemore("GushingGrapeRED",YRS,y,b);
more.show();
cout<<"Totalbottlesfor"<"<system("Pause");
return0;
}
3.无
4.
头文件
#ifndefWINE_H_
#defineWINE_H_
#include
#include
#include
#include
usingnamespacestd;
classPerson
{
private:
stringfirstname;
stringlastname;
protected:
virtualvoidData();
virtualvoidGet();
public:
Person():
firstname("noone"),lastname("noone"){}
Person(conststring&fn,conststring&ln):
firstname(fn),lastname(ln){}
virtual~Person()=0;
virtualvoidSet()=0;
virtualvoidShow()=0;
};
classGunslinger:
virtualpublicPerson
{
private:
intnumber;
protected:
voidData();
voidGet();
public:
Gunslinger():
number(0),Person(){}
Gunslinger(conststring&fn,conststring&ln,intn=0):
Person(fn,ln),number(n){}
Gunslinger(Person&p,intn=0):
Person(p),number(n){}
voidShow();
voidSet();
doubleDraw();
};
classPokerPlayer:
virtualpublicPerson
{
protected:
voidData();
public:
PokerPlayer():
Person(){}
PokerPlayer(conststring&fn,conststring&ln):
Person(fn,ln){}
PokerPlayer(constPerson&p):
Person(p){}
voidShow();
voidSet(){Person:
:
Set();}
intDraw();
};
classBadDude:
publicGunslinger,publicPokerPlayer
{
protected:
voidData();
voidGet();
public:
BadDude(){}
BadDude(intn,conststring&fn,conststring&ln):
Person(fn,ln),Gunslinger(fn,ln,n),PokerPlayer(fn,ln){}
BadDude(intn,Person&p):
Person(p),Gunslinger(p,n),PokerPlayer(p){}
BadDude(Gunslinger&g):
Person(g),Gunslinger(g),PokerPlayer(g){}
BadDude(intn,PokerPlayer&po):
Person(po),Gunslinger(po,n),PokerPlayer(po){}
doubleGdraw();
intCdraw();
voidSet();
voidShow();
};
#endif
函数定义
#include"wine.h"
Person:
:
~Person(){}
voidPerson:
:
Data()
{
cout<<"Firstname:
"<cout<<"Lastname:
"<}
voidPerson:
:
Get()
{
cout<<"Enterfirstname:
\n";
getline(cin,firstname);
cout<<"Enterlastname:
\n";
getline(cin,lastname);
}
voidPerson:
:
Set()
{
Get();
}
voidPerson:
:
Show()
{
Data();
}
voidGunslinger:
:
Data()
{
cout<<"Number:
"<}
voidGunslinger:
:
Get()
{
cout<<"Enternumber:
\n";
cin>>number;
}
voidGunslinger:
:
Show()
{
cout<<"Gunslinger\n";
Person:
:
Data();
Data();
}
voidGunslinger:
:
Set()
{
Person:
:
Get();
Get();
}
doubleGunslinger:
:
Draw()
{
returnrand()%2+3;
}
voidPokerPlayer:
:
Data()
{
cout<<"Thecard:
"<}
voidPokerPlayer:
:
Show()
{
cout<<"\nPokerPlayer:
\n";
Person:
:
Data();
Data();
}
intPokerPlayer:
:
Draw()
{
returnrand()%52+1;
}
voidBadDude:
:
Data()
{
Gunslinger:
:
Data();
PokerPlayer:
:
Data();
cout<<"\nThenextcard:
"<cout<<"ThetimeofBadDudegetthegun:
"<}
voidBadDude:
:
Get()
{
Gunslinger:
:
Get();
}
doubleBadDude:
:
Gdraw()
{
returnGunslinger:
:
Draw();
}
intBadDude:
:
Cdraw()
{
returnPokerPlayer:
:
Draw();
}
voidBadDude:
:
Set()
{
Person:
:
Get();
Get();
}
voidBadDude:
:
Show()
{
cout<<"\nBadDude:
\n";
Person:
:
Data();
Data();
}
Main
#include"wine.h"
constintSIZE=5;
intmain()
{
Person*lolas[SIZE];
intct;
for(ct=0;ct{
charchoice;
cout<<"Enterthepersoncategory:
\n"
<<"g:
Gunslingerp:
PokerPlayer"
<<"b:
Baddudeq:
Quit\n";
cin>>choice;
while(strchr("gpbq",choice)==NULL)
{
cout<<"Pleaseenterag,p,b,q:
";
cin>>choice;
}
if(choice=='q')
break;
switch(choice)
{
case'g':
lolas[ct]=newGunslinger;
break;
case'p':
lolas[ct]=newPokerPlayer;
break;
case'b':
lolas[ct]=newBadDude;
break;
}
cin.get();
lolas[ct]->Set();
}
cout<<"\nHereisperson:
\n";
inti;
for(inti=0;i{
cout<lolas[i]->Show();
}
for(i=0;ideletelolas[i];
cout<<"Done\n";
system("Pause");
return0;
}
5.
头文件
#ifndefWINE_H_
#defineWINE_H_
#include
#include
classabstr_emp
{
private:
std:
:
stringfname;
std:
:
stringlname;
std:
:
stringjob;
public:
abstr_emp();
abstr_emp(conststd:
:
string&fn,conststd:
:
string&ln,conststd:
:
string&j);
virtual~abstr_emp()=0;
virtualvoidSetall();
virtualvoidShowall()const;
friendstd:
:
ostream&operator<<(std:
:
ostream&os,constabstr_emp&e);
};
classemployee:
publicabstr_emp
{
public:
employee();
employee(conststd:
:
string&fn,conststd:
:
string&ln,conststd:
:
string&j);
virtualvoidSetall();
virtualvoidShowall()const;
};
classmanager:
virtualpublicabstr_emp
{
private:
intinchargeof;
protected:
intInChargeOf()const{returninchargeof;}
//int&InChargeOf()const{returninchargeof;}
public:
manager();
manager(conststd:
:
string&fn,conststd:
:
string&ln,conststd:
:
string&j,intico=0);
manager(constabstr_emp&e,intico=0);
manager(constmanager&m);
virtualvoidSetall();
virtualvoidShowall()const;
};
classfink:
virtualpublicabstr_emp
{
private:
std:
:
stringreportsto;
protected:
conststd:
: