14.1
//Wine.h
#include<iostream>
#include<string>
#include<valarray>
#include<utility>
typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArry;
class wine
{
private:
std::string name;
int year;
PairArry yr_bot;
public:
wine(const char*l, int y, const int yr[], const int bot[]);
wine(const char*l, int y);
wine();
~wine() {};
void GetBottles();
const std::string & Lable()const;//返回值是类型名,name不是类型,不应用&name
int sum()const;
void show()const;
};
//Wine.cpp
#include"Wine.h"
wine::wine(const char*l, int y, const int yr[], const int bot[])//yr[],bot[]是指针不能直接用于pairarry的参数传递,它的参数是两个数组
{
name = l;
year = y;
ArrayInt a(y), b(y);//将指针转换为数组作为pairarry的参数
for (int i = 0; i < y; i++)
{
a[i] = yr[i];
b[i] = bot[i];
}
yr_bot=std::make_pair(a, b);//应用yr_bot而不是PairArry,后者只是一个类型名
}
wine::wine(const char*l, int y)
{
name = l;
year = y;
ArrayInt a(y), b(y);//a,b数组的长度为y
yr_bot = std::make_pair(a, b);
}
wine::wine()
{
name = "none";
year = 0;
ArrayInt a, b;//a,b表示长度为0的int数组
yr_bot = std::make_pair(a, b);
}
void wine::GetBottles()
{
std::cout << "enter " << name << "data for " << year << "year(s):\n";
for (int i = 0; i < year; i++)
{
std::cout << "enter year: ";
std::cin >> yr_bot.first[i];
std::cout << "enter bottles for that year: ";
std::cin >> yr_bot.second[i];
}
}
const std::string &wine::Lable()const//返回值是类型名,name不是类型,不应用&name
{
return name;
}
int wine::sum()const
{
return yr_bot.second.sum();
}
void wine::show()const
{
std::cout << "wine: " << name << std::endl;
std::cout << "\t" << "Year" << "\t" << "Bottles\n";
for (int i = 0; i < year; i++)
std::cout << "\t" << yr_bot.first[i]
<< "\t" << yr_bot.second[i]
<< "\n";
}
//main.cpp
#include <iostream>
#include "wine.h"
int main()
{
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.Lable()<< ": " << more.sum() << endl; cout << "Bye\n";
return 0;
}
14.2
//Wine2.h
#include<iostream>
#include<string>
#include<valarray>
#include<utility>
typedef std::valarray<int> ArrayInt;
typedef std::pair<ArrayInt, ArrayInt> PairArry;
class wine :private std::string ,private PairArry
{
private:
int year;
public:
wine(const char*l, int y, const int yr[], const int bot[]);
wine(const char*l, int y);
wine();
~wine() {};
void GetBottles();
const std::string & Lable()const;//返回值是类型名,name不是类型,不应用&name
int sum()const;
void show()const;
};
//Wine2.cpp
#include"Wine2.h"
wine::wine(const char*l, int y, const int yr[], const int bot[])
:std::string(l),PairArry(ArrayInt(y), ArrayInt(y))//必须先使用string、pairArry的构造函数初始化
{
year = y;
for (int i = 0; i < y; i++)
{
PairArry::first[i] = yr[i];
PairArry::second[i] = bot[i];
}
}
wine::wine(const char*l, int y)
:std::string(l),PairArry(ArrayInt(y),ArrayInt(y) )
{
year = y;
}
wine::wine()
:std::string("none")
{
year = 0;
ArrayInt a, b;//a,b表示长度为0的int数组
PairArry(a, b);
}
void wine::GetBottles()
{
std::cout << "enter " << (const std::string )*this << " data for " << year << " year(s):\n";//使用强制类型转换访问基类对象
for (int i = 0; i < year; i++)
{
std::cout << "enter year: ";
std::cin >> PairArry::first[i];//使用类名加作用域解析运算符访问基类方法
std::cout << "enter bottles for that year: ";
std::cin >> PairArry::second[i];
}
}
const std::string &wine::Lable()const//返回值是类型名,name不是类型,不应用&name
{
return (const std::string&)*this;
}
int wine::sum()const
{
return PairArry::second.sum();
}
void wine::show()const
{
std::cout << "wine: " << (const std::string)*this << std::endl;
std::cout << "\t" << "Year" << "\t" << "Bottles\n";
for (int i = 0; i < year; i++)
std::cout << "\t" << PairArry::first[i]
<< "\t" << PairArry::second[i]
<< "\n";
}
//Wine.cpp
#include"Wine2.h"
wine::wine(const char*l, int y, const int yr[], const int bot[])
:std::string(l),PairArry(ArrayInt(y), ArrayInt(y))//必须先使用string、pairArry的构造函数初始化
{
year = y;
for (int i = 0; i < y; i++)
{
PairArry::first[i] = yr[i];
PairArry::second[i] = bot[i];
}
}
wine::wine(const char*l, int y)
:std::string(l),PairArry(ArrayInt(y),ArrayInt(y) )
{
year = y;
}
wine::wine()
:std::string("none")
{
year = 0;
ArrayInt a, b;//a,b表示长度为0的int数组
PairArry(a, b);
}
void wine::GetBottles()
{
std::cout << "enter " << (const std::string )*this << " data for " << year << " year(s):\n";//使用强制类型转换访问基类对象
for (int i = 0; i < year; i++)
{
std::cout << "enter year: ";
std::cin >> PairArry::first[i];//使用类名加作用域解析运算符访问基类方法
std::cout << "enter bottles for that year: ";
std::cin >> PairArry::second[i];
}
}
const std::string &wine::Lable()const//返回值是类型名,name不是类型,不应用&name
{
return (const std::string&)*this;
}
int wine::sum()const
{
return PairArry::second.sum();
}
void wine::show()const
{
std::cout << "wine: " << (const std::string)*this << std::endl;
std::cout << "\t" << "Year" << "\t" << "Bottles\n";
for (int i = 0; i < year; i++)
std::cout << "\t" << PairArry::first[i]
<< "\t" << PairArry::second[i]
<< "\n";
}
14.3
//worker.h
#include <string>
class Worker
{
private:
std::string fullname;
long id;
protected:
virtual void Data() const;
virtual void Get();
public:
Worker() : fullname("no one"), 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 &wk, int p = 0) : Worker(wk), panache(p) {}
void Set();
void Show() const;
};
class Singer : virtual public Worker
{
protected:
enum { other, alto, contralto, soprano, bass, baritone, tenor };
enum { Vtypes = 7 };
void Data() const;
void Get();
private:
static const char *pv[Vtypes];
int voice;
public:
Singer() : Worker(), voice(other) {}
Singer(const std::string &s, long n, int v = other) : Worker(s, n), voice(v) {}
Singer(const Worker &wk, int v = other) : Worker(wk), voice(v) {}
void Set();
void Show() const;
};
class SingingWaiter : public Singer, public Waiter
{
protected:
void Data() const;
void Get();
public:
SingingWaiter() {}
SingingWaiter(const std::string &s, long n, int p = 0, int v = other)
: Worker(s, n), Waiter(s, n, p), Singer(s, n, v) {}
SingingWaiter(const Worker &wk, int p = 0, int v = other)
: Worker(wk), Waiter(wk, p), Singer(wk, v) {}
SingingWaiter(const Waiter &wt, int v = other)
: Worker(wt), Waiter(wt), Singer(wt, v) {}
SingingWaiter(const Singer &wt, int p = 0)
: Worker(wt), Waiter(wt, p), Singer(wt) {}
void Set();
void Show() const;
};
//worker.cpp
#include<iostream>
#include "worker.h"
using std::cout;
using std::cin;
using std::endl;
Worker::~Worker() {}
void Worker::Data() const
{
cout << "Name: " << fullname << endl;
cout << "Emplyee ID: " << id << endl;
}
void Worker::Get()
{
getline(cin, fullname);
cout << "Enter worker's ID: ";
cin >> id;
while (cin.get() != '\n')
continue;
}
void Waiter::Set()
{
cout << "Enter waiter's name: ";
Worker::Get();
Get();
}
void Waiter::Show() const
{
cout << "Category: waiter\n";
Worker::Data();
Data();
}
void Waiter::Data() const
{
cout << "Panache rating: " << panache << endl;
}
void Waiter::Get()
{
cout << "Enter waiter's panache rating: ";
cin >> panache;
while (cin.get() != '\n')
continue;
}
const char *Singer::pv[Singer::Vtypes] = { "other", "alto", "contralto", "soprano", "bass", "baritone", "tenor" };
void Singer::Set()
{
cout << "Enter singer's name: ";
Worker::Get();
Get();
}
void Singer::Show() const
{
cout << "Category: singer\n";
Worker::Data();
Data();
}
void Singer::Data() const
{
cout << "Vocal range: " << pv[voice] << endl;
}
void Singer::Get()
{
cout << "Enter number for singer's vocal range:\n";
int i;
for (i = 0; i < Vtypes; ++i)
{
cout << i << ": " << pv[i] << " ";
if (i % 4 == 3)
cout << endl;
}
if (i % 4 != 0)
cout << '\n';
cin >> voice;
while (cin.get() != '\n')
continue;
}
void SingingWaiter::Data() const
{
Singer::Data();
Waiter::Data();
}
void SingingWaiter::Get()
{
Waiter::Get();
Singer::Get();
}
void SingingWaiter::Set()
{
cout << "Enter singing waiter's name: ";
Worker::Get();
Get();
}
void SingingWaiter::Show() const
{
cout << "Category: singing waiter\n";
Worker::Data();
Data();
}
//QueueTp.h
template<class Type>
class Queue
{
private:
struct Node { Type item; struct Node *next; };
enum {SIZE =10};
int queuesize;
Node *front;
Node *rear;
int items;
Queue(const Queue&) :queuesize(0) {};
Queue &operator=(const Queue& q) { return *this; };
public:
explicit Queue(int s = SIZE);
~Queue();
int queuecount() { return items; };
bool isempty() { return items == 0; };
bool isfull() { return items == queuesize; };
Type dequeue();
bool enqueue(Type item);
};
template<class Type>
Queue<Type>::Queue(int ss):queuesize(ss)
{
front = rear = nullptr;
items = 0;
}
template<class Type>
Queue<Type>::~Queue()
{
Node *temp;
while (front!=nullptr)
{
temp = front;
front = front->next;
delete temp;
}
}
template<class Type>
Type Queue<Type>::dequeue( )
{
if (front == nullptr)
return false;
Type t=front->item;
items--;
Node *temp = front;
front = front->next;
delete temp;
if (items == 0)
rear = nullptr;
return t;
}
template<class Type>
bool Queue<Type>::enqueue(Type item)
{
if (isfull())
return false;
Node *add = new Node;
add->item = item;
add->next = nullptr;
items++;
if (front == nullptr)
front = add;
else
rear->next = add;
rear = add;
return true;
}
//main.cpp
#include <iostream>
#include <cstring>
#include "worker.h"
#include "QueueTp.h"
const int SIZE = 2;
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::strchr;
Queue<Worker *> lolas(SIZE);//显示模板实例化
int ct;
for (ct = 0; ct < SIZE; ++ct)
{
char choice;
cout << "Enter the employee category: \n"
<< "w: waiter s: singer "
<< "t: singing waiter q: quit\n";
cin >> choice;
while (strchr("wstq", choice) == NULL)
{
cout << "Please enter a w, s, t, or q: ";
cin >> choice;
}
if (choice == 'q')
break;
cin.get();
//若要在case里面定义新变量需要用{}将语句括起来
switch (choice)
{
case 'w':
{
Waiter *t1 = new Waiter;
t1->Set();
lolas.enqueue(t1);
}
break;
case 's':
{
Singer *t2 = new Singer;
t2->Set();
lolas.enqueue(t2);
}
break;
case 't':
{
SingingWaiter *t3 = new SingingWaiter;
t3->Set();
lolas.enqueue(t3);
}
break;
}
}
cout << "\nHere is your staff:\n";
int i;
for (i = 0; i < ct; ++i)
{
cout << endl;
Worker *temp = lolas.dequeue();
temp->Show();
}
cout << "Bye";
return 0;
}
14.4
//14.4h
#include<string>
using std::string;
class Person
{
private:
string firstname;
string secondname;
public:
Person(const string &f="none", const string &s="empty");
virtual~Person() {};
virtual void set() = 0;
virtual void show()=0;
};
class Gunslinger :virtual public Person
{
private:
double time;
int nick;
public:
Gunslinger(const string &f = "none", const string &s = "empty", double t=0.0, int n=0);
Gunslinger(double t, int n, const Person&p);
Gunslinger(const Gunslinger&g);
~Gunslinger() {};
double Draw() { return time; };
void set();
void show();
};
class PokerPlayer :virtual public Person
{
public:
~PokerPlayer() {};
int Draw();
void set();
void show() ;
};
class BadDude :public Gunslinger, public PokerPlayer
{
public:
BadDude(const string &f = "none", const string &s = "empty", double t = 0.0, int n = 0);
BadDude(const Person &p, double t, int n);
BadDude(const Gunslinger &g);
BadDude(double t, int n, PokerPlayer &p);
~BadDude() {};
double Gdraw();
int Cdraw();
void set();
void show();
};
//14.4cpp
#include<iostream>
#include<cstring>
#include<cstdlib>//rand(),srand()头文件
#include<ctime>//time()头文件
#include"14.4.h"
//Person method
Person::Person(const string &f, const string &s)
{
firstname = f;
secondname = s;
}
void Person::set()
{
std::cout << "enter firstname: ";
std::getline(std::cin, firstname);
std::cout << "enter secondname: ";
std::getline(std::cin, secondname);
}
void Person::show()
{
std::cout << secondname << "." << firstname << std::endl;
}
//Gunlinger method
Gunslinger::Gunslinger(const string &f, const string &s, double t, int n)
:Person::Person(f, s)
{
time = t;
nick = n;
}
Gunslinger::Gunslinger(double t, int n, const Person&p)
:Person(p)
{
time = t;
nick = n;
}
Gunslinger::Gunslinger(const Gunslinger&g)
:Person(g)
{
time = g.time;
nick = g.nick;
}
void Gunslinger::set()
{
Person::set();
std::cout << "enter time: ";
std::cin >> time;
std::cout << "enter nick: ";
std::cin >> nick;
}
void Gunslinger::show()
{
Person::show();
std::cout << "time: " << time<<std::endl
<< "nicke: " << nick << std::endl;
}
//PokerPlayer method
int PokerPlayer::Draw()
{
//srand(time(0));
return (rand() % 51 + 1);
}
void PokerPlayer::set()
{
Person::set();
}
void PokerPlayer::show()
{
Person::show();
std::cout << "poker: " << Draw();
}
//BadDude method
BadDude::BadDude(const string &f, const string &s, double t, int n)
:Person(f, s), Gunslinger(f, s, t, n) {};
BadDude::BadDude(const Person &p, double t, int n)
:Person(p), Gunslinger(t, n, p) {};
BadDude::BadDude(const Gunslinger &g)
:Gunslinger(g) {};//虚基类构造函数要采用虚基类,不要Person(g)是否可以
BadDude::BadDude(double t, int n, PokerPlayer &p)
:Person(p), Gunslinger(t, n, p) {};
double BadDude::Gdraw()
{
return Gunslinger::Draw();
}
int BadDude::Cdraw()
{
return PokerPlayer::Draw();
}
void BadDude::set()
{
Gunslinger::set();
}
void BadDude::show()
{
Gunslinger::show();
std::cout <<"poker: "<<PokerPlayer::Draw()<<std::endl
<<"next poker:"<<Cdraw()<<std::endl;
}
//main.cpp
#include <iostream>
#include <cstring>
#include "14.4.h"
const int SIZE = 2;
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::strchr;
Person *p[SIZE];//基类指针数值可以将派生类赋给基类指针
int ct;
for (ct = 0; ct < SIZE; ++ct)
{
char choice;
cout << "Enter the employee category: \n"
<< "g: Gunslinger p: PokerPlayer "
<< "b:BadDude q: quit\n";
cin >> choice;
while (strchr("gpbq", choice) == NULL)
{
cout << "Please enter g p, b or q: ";
cin >> choice;
}
if (choice == 'q')
break;
switch (choice)
{
case 'g':p[ct] = new Gunslinger;
break;
case 'p':p[ct] = new PokerPlayer;
break;
case 'b':p[ct] = new BadDude;
break;
}
cin.get();
p[ct]->set();
}
std::cout << "result:\n";
for (int i = 0; i < ct; ++i)
{
p[i]->show();
}
cout << "Bye";
return 0;
}
14.5
// emp.h
#pragma once
#include <iostream>
#include <string>
class abstr_emp
{
public:
abstr_emp();
abstr_emp(const std::string & fn, const std::string & ln,
const std::string & j);
virtual void ShowAll() const;
virtual void SetAll();
friend std::ostream &
operator<<(std::ostream & os, const abstr_emp & e);
virtual ~abstr_emp() = 0;
private:
std::string fname;
std::string lname;
std::string job;
};
class employee : public abstr_emp
{
public:
employee();
employee(const std::string & fn, const std::string & ln,
const std::string & 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();
manager(const std::string & fn, const std::string & ln,
const std::string & j, int ico = 0);
manager(const abstr_emp & e, int ico);
manager(const manager & m);
virtual void ShowAll() const;
virtual void SetAll();
};
class fink : virtual public abstr_emp
{
private:
std::string reportsto;
protected:
const std::string ReportsTo() const { return reportsto; }
std::string & ReportsTo() { return reportsto; }
public:
fink();
fink(const std::string & fn, const std::string & ln,
const std::string & j, const std::string & rpo);
fink(const abstr_emp & e, const std::string & rep);
fink(const fink & e);
virtual void ShowAll() const;
virtual void SetAll();
};
class highfink : public manager, public fink
{
public:
highfink();
highfink(const std::string & fn, const std::string & ln,
const std::string & j, const std::string & rpo,
int ico);
highfink(const abstr_emp & e, const std::string & rpo, int ico);
highfink(const fink & f, int ico);
highfink(const manager & m, const std::string & rpo);
highfink(const highfink & h);
virtual void ShowAll() const;
virtual void SetAll();
};
//emp.cpp
#include<iostream>
#include"emp.h"
using std::cout;
using std::cin;
using std::endl;
//abstr_emp method
abstr_emp::abstr_emp()
{
fname=lname=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
<< "\nlast name: " << lname
<< "\njob: " << job<<endl;
}
void abstr_emp::SetAll()
{
cout << "enter first name: ";
getline(cin, fname);
cout << "enter last name: ";
getline(cin, lname);
cout << "enter job: ";
getline(cin, job);
}
std::ostream &operator<<(std::ostream & os, const abstr_emp & e)
{
os << e.fname << "." << e.lname << endl;
return os;
}
abstr_emp::~abstr_emp() {};
//employee method
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 method
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)
{
inchargeof = m.inchargeof;
}
void manager::ShowAll() const
{
abstr_emp::ShowAll();
cout << "inchargeof: " << inchargeof << endl;
}
void manager::SetAll()
{
abstr_emp::SetAll();
cout << "enter inchargeof: ";
cin >> inchargeof;
}
//fink method
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 & rep)
:abstr_emp(e)
{
reportsto = rep;
}
fink::fink(const fink & e)
:abstr_emp(e)
{
reportsto = e.reportsto;
}
void fink::ShowAll() const
{
abstr_emp::ShowAll();
cout << "reportsto: " << reportsto<<endl;
}
void fink::SetAll()
{
abstr_emp::SetAll();
cout << "enter reportsto: ";
cin >> reportsto;
}
//highfink method
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 << "InChangeOf: " << InChargeOf() << endl
<< "reportsto: " << ReportsTo() << endl;
}
void highfink::SetAll()
{
manager::SetAll();
cin.get();//吸收输入int型inchargeof后的换行符
cout << "enter ReportsTo: ";
getline(cin, fink::ReportsTo());
}
//main.cpp
#include <iostream>
#include"emp.h"
using std::cout;
using std::cin;
using std::endl;
int main()
{
employee em("Trip", "Harris", "Thumper");
cout << em << endl;
em.ShowAll();
manager ma("Amorphias", "Spindragon", "Nuancer", 5);
cout << ma << endl;
ma.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";
cin.get();
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;
}
版权声明:本文为Schlangemm原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。