#ifndef WINEC_H_
#define WINEC_H_
#include<string>
#include<valarray>
using namespace std;
template <typename T1, typename T2>
class Pair
{
private:
T1 year;
T2 bottles;
public:
Pair() {}
Pair(const T1& yr, const T2& bt) : year(yr), bottles(bt) {}
void Set(const T1& yr, const T2& bt);
int Sum() const;
void Show(int y) const;
};
template <typename T1, typename T2>
void Pair<T1, T2>::Set(const T1& yr, const T2& bt)
{
year = yr;
bottles = bt;
}
template <typename T1, typename T2>
int Pair<T1, T2>::Sum() const
{
return bottles.sum();
}
template <typename T1, typename T2>
void Pair<T1, T2>::Show(int y) const
{
for (int i = 0; i < y; i++)
{
cout << '\t' << year[i] << '\t' << bottles[i] << endl;
}
return;
}
typedef valarray<int> ArraryInt;
typedef Pair<ArraryInt, ArraryInt> PairArray;
class Wine
{
private:
string name;
PairArray year_bottle;
int year;
public:
Wine(const char*l, int y) ;
Wine(const char* l, int y, const int yr[], const int bot[]) ;
~Wine();
void GetBottles() ;
string &Label() ;
int sum() ;
void Show()const;
};
#endif // !WINEC_H_
#include<iostream>
#include"14_1.h"
using namespace std;
Wine::Wine(const char* l, int y)
{
name = l;
year_bottle.Set(ArraryInt(y),ArraryInt(y));
}
Wine::Wine(const char* l, int y, const int yr[], const int bot[])
{
name = l;
year_bottle.Set(ArraryInt(yr, y), ArraryInt(bot, y));
year = y;
}
Wine::~Wine()
{}
void Wine::GetBottles()
{
ArraryInt yr(year);
ArraryInt bt(year);
cout << "Enetr " << name << " data for " << year << " year(s):" << endl;
for (int i = 0; i < year; i++)
{
cout << "Enter year:";
cin >> yr[i];
cout << "Enter bottles:";
cin >> bt[i];
}
year_bottle.Set(yr, bt);
}
string& Wine::Label()
{
return name;
}
int Wine::sum()
{
return year_bottle.Sum();
}
void Wine::Show()const
{
cout << "Wine: " << name << endl;
cout << "\tYear\tBottles" << endl;
year_bottle.Show(year);
}
#include<iostream>
#include"14_1.h"
int main(void)
{
using std::cin;
using std::cout;
using std::endl;
cout << "Enter name of wine: ";
char lab[50];
cin.getline(lab, 50);
cout << "Enter number 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("Gushing Grape Red", YRS, y, b);
more.Show();
cout << "Total bottles for " << more.Label() << ":" << more.sum() << endl;
cout << "Bye\n";
return 0;
}
#ifndef WINEC_H_
#define WINEC_H_
#include<string>
#include<valarray>
using namespace std;
template <typename T1, typename T2>
class Pair
{
private:
T1 year;
T2 bottles;
public:
Pair() {}
Pair(const T1& yr, const T2& bt) : year(yr), bottles(bt) {}
void Set(const T1& yr, const T2& bt);
int Sum() const;
void Show(int y) const;
};
template <typename T1, typename T2>
void Pair<T1, T2>::Set(const T1& yr, const T2& bt)
{
year = yr;
bottles = bt;
}
template <typename T1, typename T2>
int Pair<T1, T2>::Sum() const
{
return bottles.sum();
}
template <typename T1, typename T2>
void Pair<T1, T2>::Show(int y) const
{
for (int i = 0; i < y; i++)
{
cout << '\t' << year[i] << '\t' << bottles[i] << endl;
}
return;
}
typedef valarray<int> ArraryInt;
typedef Pair<ArraryInt, ArraryInt> PairArray;
class Wine:private string,private PairArray
{
private:
int year;
public:
Wine(const char* l, int y);
Wine(const char* l, int y, const int yr[], const int bot[]);
~Wine();
void GetBottles();
string& Label();
int sum();
void Show()const;
};
#endif // !WINEC_H_
#include<iostream>
#include"14_2.h"
using namespace std;
Wine::Wine(const char* l, int y):string(l)
{
PairArray::operator=(PairArray(ArraryInt(y), ArraryInt(y)));
year = y;
}
Wine::Wine(const char* l, int y, const int yr[], const int bot[]):string(l)
{
PairArray::operator=(PairArray(ArraryInt(yr,y), ArraryInt(bot,y)));
year = y;
}
Wine::~Wine()
{}
void Wine::GetBottles()
{
ArraryInt yr(year);
ArraryInt bt(year);
cout << "Enetr " << (const string&)*this << " data for " << year << " year(s):" << endl;
for (int i = 0; i < year; i++)
{
cout << "Enter year:";
cin >> yr[i];
cout << "Enter bottles:";
cin >> bt[i];
}
PairArray::operator=(PairArray(yr, bt));
}
string& Wine::Label()
{
return (string&)*this; //使用强制类型转换访问私有对象;
}
int Wine::sum()
{
return Pair::Sum();
}
void Wine::Show()const
{
cout << "Wine: " << (const string&)*this << endl;
cout << "\tYear\tBottles" << endl;
Pair::Show(year);
}
#include<iostream>
#include"14_2.h"
int main(void)
{
using std::cin;
using std::cout;
using std::endl;
cout << "Enter name of wine: ";
char lab[50];
cin.getline(lab, 50);
cout << "Enter number 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("Gushing Grape Red", YRS, y, b);
more.Show();
cout << "Total bottles for " << more.Label() << ":" << more.sum() << endl;
cout << "Bye\n";
return 0;
}
#ifndef QUEUETP_H_
#define QUEUETP_H_
template <typename Item>
class QueueTp
{
private:
enum{ Q_SIZE = 10 };
struct Node
{
Item item;
struct Node *next;
};
Node *front;
Node *rear;
int items;
const int qsize;
QueueTp(const QueueTp &q) : qsize(0) {}
QueueTp &operator=(const QueueTp &q) { return *this; }
public:
QueueTp(int qs = Q_SIZE);
~QueueTp();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const Item &item);
bool dequeue(Item &item);
};
template <typename Item>
QueueTp<Item>::QueueTp(int qs) : qsize(qs)
{
front = rear = nullptr;
items = 0;
}
template <typename Item>
QueueTp<Item>::~QueueTp()
{
Node *temp;
while (front != nullptr)
{
temp = front;
front = front->next;
delete temp;
}
}
template <typename Item>
bool QueueTp<Item>::isempty() const
{
return 0 == items;
}
template <typename Item>
bool QueueTp<Item>::isfull() const
{
return qsize == items;
}
template <typename Item>
int QueueTp<Item>::queuecount() const
{
return items;
}
template <typename Item>
bool QueueTp<Item>::enqueue(const Item &item)
{
if (isfull())
{
return false;
}
Node *add = new Node;
add->item = item;
add->next = nullptr;
++items;
if (nullptr == front)
{
front = add;
}
else
{
rear->next = add;
}
rear = add;
return true;
}
template <typename Item>
bool QueueTp<Item>::dequeue(Item &item)
{
if (isempty())
{
return false;
}
item = front->item;
--items;
Node *temp = front;
front = front->next;
delete temp;
if (0 == items)
{
rear = nullptr;
}
return true;
}
#endif
#ifndef WORKER_H_
#define WORKER_H_
#include <string>
class Worker
{
private:
std::string fullname;
long id;
public:
Worker() : fullname("no one"), id(0L) {}
Worker(const std::string &s, long n) : fullname(s), id(n) {}
~Worker();
void Set();
void Show() const;
};
#endif
#ifndef PERSONMI_H_
#define PERSONMI_H_
#include <string>
using std::string;
class Person
{
private:
string firstname;
string lastname;
protected:
virtual void Data() const; //虚保护方法打印基类成员信息, 使得派生类可以间接访问;
virtual void Get(); //虚保护方法获取基类成员信息, 使得派生类可以间接访问;
public:
Person() : firstname("no"), lastname("one") {}
Person(const string &fname, const string &lname) : firstname(fname), lastname(lname) {}
virtual ~Person() = 0;
virtual void Set() = 0;
virtual void Show() const = 0;
};
class Gunslinger : virtual public Person
{
private:
int nicks;
protected:
void Data() const; //重新定义保护方法;
void Get();
public:
Gunslinger() : Person(), nicks(0) {}
Gunslinger(const string &f, const string &l, int n) : Person(f, l), nicks(n) {}
Gunslinger(const Person &p, int n) : Person(p), nicks(n) {}
void Set();
void Show() const;
double Draw() const; //打印枪手的拔枪时间;
};
class PokerPlayer : virtual public Person
{
protected:
void Data() const; //重新定义保护方法;
public:
PokerPlayer() : Person("no", "one") {}
PokerPlayer(const string &f, const string &l) : Person(f, l) {}
PokerPlayer(const Person &p) : Person(p) {}
void Set();
void Show() const;
int Draw() const; //表示扑克牌的值;
};
class BadDude : public Gunslinger, public PokerPlayer
{
protected:
void Data() const; //重新定义保护方法;
void Get();
public:
BadDude() {}
BadDude(const string &f, const string &l, int n) : Person(f, l), Gunslinger(f, l, n), PokerPlayer(f, l) {}
BadDude(const Person &p, int n) : Person(p), Gunslinger(p, n), PokerPlayer(p) {}
BadDude(const Gunslinger &g) : Person(g), Gunslinger(g), PokerPlayer(g) {}
BadDude(const PokerPlayer &p, int n) : Person(p), Gunslinger(p, n), PokerPlayer(p) {}
void Set();
void Show() const;
double Gdraw() const; //打印坏蛋拔枪的时间;
int Cdraw() const; //打印下一张扑克牌的值;
};
#endif
#include <iostream>
#include <cstdlib>
#include "personmi.h"
using namespace std;
Person::~Person()
{
}
void Person::Data() const
{
cout << "First name: " << firstname << endl;
cout << "Last name: " << lastname << endl;
return;
}
void Person::Get()
{
cout << "Please enter your first name: ";
getline(cin, firstname);
cout << "Please enter your last name: ";
getline(cin, lastname);
return;
}
void Gunslinger::Data() const
{
cout << "Gunslinger nicks: " << nicks << endl;
return;
}
void Gunslinger::Get()
{
cout << "Please enter the nicks for gunslinger: ";
cin >> nicks;
while (cin.get() != '\n')
continue;
return;
}
void Gunslinger::Set()
{
cout << "Enter Gunslinger name" << endl;
Person::Get();
Get();
return;
}
void Gunslinger::Show() const
{
cout << "Category: Gunslinger" << endl;
Person::Data();
Data();
return;
}
double Gunslinger::Draw() const
{
return double(rand() % 5 + 1); //枪手的拔枪时间设置为1到5之间的值;
}
void PokerPlayer::Data() const
{
cout << "The cards: " << Draw() << endl;
return;
}
void PokerPlayer::Set()
{
cout << "Enter PokerPlayer name" << endl;
Person::Get();
return;
}
void PokerPlayer::Show() const
{
cout << "Category: PokerPlayer" << endl;
Person::Data();
Data();
return;
}
int PokerPlayer::Draw() const //扑克牌的值;
{
return rand() % 52 + 1;
}
void BadDude::Data() const
{
Gunslinger::Data();
PokerPlayer::Data();
cout << "The time for a bad guy to draw his gun is " << Gdraw() << endl;
cout << "The next card is " << Cdraw() << endl;
return;
}
void BadDude::Get()
{
Gunslinger::Get();
return;
}
void BadDude::Set()
{
cout << "Enter BadDude name" << endl;
Person::Get();
Get();
return;
}
void BadDude::Show() const
{
cout << "Category: BadDude" << endl;
Person::Data();
Data();
return;
}
double BadDude::Gdraw() const
{
return Gunslinger::Draw();
}
int BadDude::Cdraw() const
{
return PokerPlayer::Draw();
}
#include <iostream>
#include <cstring>
#include "personmi.h"
const int SIZE = 5;
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::strchr;
int i, ct;
Person *people[SIZE];
for (ct = 0; ct < SIZE; ct++)
{
char choice;
cout << "Enter the person category:" << endl;
cout << "g: gunslinger" << endl;
cout << "p: pokerplayer" << endl;
cout << "b: baddude" << endl;
cout << "q: quit" << endl;
cin >> choice;
while (NULL == strchr("bgpq", choice))
{
cout << "Please enter b, g, p or q: ";
cin >> choice;
}
if ('q' == choice)
{
break;
}
switch (choice)
{
case 'b':
{
people[ct] = new BadDude;
break;
}
case 'g':
{
people[ct] = new Gunslinger;
break;
}
case 'p':
{
people[ct] = new PokerPlayer;
break;
}
}
cin.get();
people[ct]->Set();
}
cout << "\nHere is your message for some people:" << endl;
for (i = 0; i < ct; i++)
{
cout << endl;
people[i]->Show();
}
for (i = 0; i < ct; i++)
{
delete people[i];
}
cout << "Bye.\n";
return 0;
}
#include"14_5.h"
#include<iostream>
#include<string>
using namespace std;
abstr_emp::abstr_emp() :fname("None"), lname("None"), job("None") {}
abstr_emp::abstr_emp(const std::string& fn, const std::string& ln, const std::string& j) : fname(fn), lname(ln), job(j) {}
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 first name:";
cin >> fname;
cout << "Enter last name:";
cin >> lname;
cout << "Enter job:";
cin >> job;
}
std::ostream& operator<<(std::ostream& os, const abstr_emp& e)
{
os << "First name:" << e.fname << endl;
os << "Last name:" << e.lname << endl;
os << "Job:" << e.job << endl;
return os;
}
abstr_emp:: ~abstr_emp() {}
employee::employee() :abstr_emp() {};
employee::employee(const std::string& fn, const std::string& ln, const std::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 std::string& fn, const std::string& ln, const std::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) {}
void manager::ShowAll() const
{
abstr_emp::ShowAll();
cout << "Inchargeof:" << inchargeof << endl;
}
void manager::SetAll()
{
abstr_emp::SetAll();
cout << "Enter inchargeof:";
cin >> inchargeof;
}
fink::fink() :abstr_emp(), reportsto("None") {}
fink::fink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo) : abstr_emp(fn, ln, j), reportsto(rpo) {}
fink::fink(const abstr_emp& e, const std::string& rpo) : abstr_emp(e), reportsto(rpo) {}
fink::fink(const fink& e) : abstr_emp(e) {}
void fink::ShowAll() const
{
abstr_emp::ShowAll();
cout << "Reportsto:" << reportsto << endl;
}
void fink::SetAll()
{
abstr_emp::SetAll();
cout << "Enter reportsto:";
cin >> reportsto;
}
highfink::highfink() :abstr_emp(),manager(), fink() {}
highfink::highfink(const std::string& fn, const std::string& ln, const std::string& j, const std::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 std::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 std::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
{
abstr_emp::ShowAll();
cout << "In charge of: " << manager::InChargeOf() << endl;
cout << "Reports to: " << fink::ReportsTo() << endl;
}
void highfink::SetAll()
{
abstr_emp::SetAll();
cout << "Please enter a number for inchargerof: ";
cin >> InChargeOf();
while (cin.get() != '\n')
continue;
cout << "Please enter a string for reportsto: ";
getline(cin, ReportsTo());
}
(1):因为没有使用new进行动态内存分配因此使用的是隐式赋值运算符。
(2):在派生类中进行了再次定义,所以声明为虚函数。
(3):使得从多个类(基类相同)派生出的对象只继承1个基类对象。
(4):没有新增的数据成员,继承过来的数据成员需用基类方法间接访问。
(5):因为只访问对象中的3个数据部分,其余部分未进行访问。
(6):编译器将会报错,抽象基类不能进行实例化,但可以使用抽象基类的指针来访问派生出来的多个不同类。