1:
//winec.h
#ifndef WINE_H_
#define WINE_H_
#include<iostream>
#include<valarray>
#include<string>
template<class Y, class B>
class Pair
{
private:
Y year;
B bottle;
public:
Pair(){};
Pair(const Y &y, const B &b):year(y), bottle(b){};
int sumPair()const;
void setPair(const Y &y, const B &b);
void showPair(int n)const;
};
template<class Y, class B>
int Pair<Y, B>::sumPair()const
{
return bottle.sum();
}
template<class Y, class B>
void Pair<Y, B>::setPair(const Y &y, const B &b)
{
year = y;
bottle = b;
}
template<class Y, class B>
void Pair<Y, B>::showPair(int n)const
{
for(int i = 0; i < n; i++)
{
std::cout << " " << year[i] << " " << bottle[i] << std::endl;
}
}
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
private:
std::string name;
PairArray yb;
int yrs;
public:
Wine(){};
Wine(const char *l, int y, const int yr[], const int bot[]);
Wine(const char *l, int y);
~Wine(){};
void getBottles();
int sum()const;
std::string & label();
void show()const;
};
#endif
//winec.cpp
#include<iostream>
#include"winec.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
Wine::Wine(const char *l, int y, const int yr[], const int bot[])
{
name = l;
yrs = y;
yb.setPair(ArrayInt(yr, yrs), ArrayInt(bot, yrs));
}
Wine::Wine(const char *l, int y)
{
name = l;
yrs = y;
}
void Wine::getBottles()
{
ArrayInt yr(yrs), bt(yrs);
for(int i = 0; i < yrs; i++)
{
cout << "Enter year : ";
cin >> yr[i];
cout << "Enter bottles for that year : ";
cin >> bt[i];
}
while(cin.get() != '\n')
{
continue;
}
yb.setPair(yr, bt);
}
string & Wine::label()
{
return name;
}
void Wine::show()const
{
cout << "Wine name : " << name << endl;
cout << " Year : " << " Bottle : " << endl;
yb.showPair(yrs);
}
int Wine::sum()const
{
return yb.sumPair();
}
//main.cpp
#include<iostream>
#include"winec.h"
using namespace std;
int main()
{
cout << "Enter name of wine : ";
char lab[50];
cin.getline(lab, 50);
cout << "Enter the 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] = {49, 60 ,72};
Wine more("Gushing Graph Red", YRS, y, b);
more.show();
cout << "Total bottles for " << more.label() << " : " << more.sum() << endl;
cout << "done . " << endl;
return 0;
}
2:
//winec.h
#ifndef WINEC_H_
#define WINEC_H_
#include<iostream>
#include<valarray>
#include<string>
using std::string;
using std::valarray;
using std::cout;
using std::endl;
template<class Y, class B>
class Pair
{
private:
Y year;
B bottle;
public:
Pair(){};
Pair(const Y &y, const B &b):year(y), bottle(b){};
~Pair(){};
int sumPair()const;
void showPair(int n)const;
void setPair(const Y &y, const B &b);
};
template<class Y, class B>
int Pair<Y, B>::sumPair()const
{
return bottle.sum();
}
template<class Y, class B>
void Pair<Y, B>::showPair(int n)const
{
for(int i = 0; i < n; i++)
{
cout << " " << year[i] << " " << bottle[i] << endl;
}
}
template<class Y, class B>
void Pair<Y, B>::setPair(const Y &y, const B &b)
{
year = y;
bottle = b;
}
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine:private PairArray, private string
{
private:
int yrs;
public:
Wine(){};
Wine(const char *l, int y, const int yr[], const int bot[]);
Wine(const char *l, int y);
~Wine(){};
void getBottles();
string & label();
int sum()const;
void show()const;
};
#endif
//winec.cpp
#include<iostream>
#include"winec.h"
using std::cin;
using std::cout;
using std::endl;
Wine::Wine(const char *l, int y, const int yr[], const int bot[]):string(l), yrs(y), PairArray(ArrayInt(yr, y), ArrayInt(bot, y))
{
}
Wine::Wine(const char *l, int y):string(l), yrs(y)
{
}
void Wine::getBottles()
{
ArrayInt yr(yrs), bt(yrs);
for(int i = 0; i < yrs; i++)
{
cout << "Enter year : ";
cin >> yr[i];
cout << "Enter bottles for that year : ";
cin >> bt[i];
}
while(cin.get() != '\n')
{
continue;
}
PairArray::setPair(yr, bt);
}
string & Wine::label()
{
return (string &)*this;
}
int Wine::sum()const
{
return PairArray::sumPair();
}
void Wine::show()const
{
cout << "Wine : " << (string &)*this << endl;
cout << " " << "Year" << " " << "Bottles" << endl;
PairArray::showPair(yrs);
}
//main.cpp
#include<iostream>
#include"winec.h"
using namespace std;
int main()
{
cout << "Enter name of wine : ";
char lab[50];
cin.getline(lab, 50);
cout << "Enter the 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] = {49, 60 ,72};
Wine more("Gushing Graph Red", YRS, y, b);
more.show();
cout << "Total bottles for " << more.label() << " : " << more.sum() << endl;
cout << "done . " << endl;
return 0;
}
3:
//queuetp.h
#ifndef QUEUETP_H_
#define QUEUETP_H_
#include<iostream>
#include<string>
#include<cstring>
using std::string;
class Worker
{
private:
string name;
long id;
public:
Worker():name("Null"), id(0000){};
Worker(const string &s, long num):name(s), id(num){};
~Worker(){};
void set();
void show()const;
};
template<typename T>
class QueueTp
{
private:
struct Node
{
T item;
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 = 10);
~QueueTp();
bool isempty()const;
bool isfull()const;
int queueCount()const;
bool enqueue(const T &item);
bool dequeue(T &item);
};
template<typename T>
QueueTp<T>::QueueTp(int qs):qsize(qs)
{
front = rear = NULL;
items = 0;
}
template<typename T>
QueueTp<T>::~QueueTp()
{
Node *temp;
while(front != NULL)
{
temp = front;
front = front -> next;
delete temp;
}
}
template<typename T>
bool QueueTp<T>::isempty()const
{
return items == 0;
}
template<typename T>
bool QueueTp<T>::isfull()const
{
return items == qsize;
}
template<typename T>
int QueueTp<T>::queueCount()const
{
return items;
}
template<typename T>
bool QueueTp<T>::enqueue(const T &item)
{
if(isfull())
{
return false;
}
Node *add = new Node;
add->item = item;
add->next = NULL;
items++;
if(front == NULL)
{
front = add;
}
else
{
rear->next = add;
}
rear = add;
return true;
}
template<typename T>
bool QueueTp<T>::dequeue(T &item)
{
if(front == NULL)
{
return false;
}
item = front->item;
items--;
Node *temp;
temp = front;
front = front->next;
delete temp;
if(items == 0)
{
rear = NULL;
}
return true;
}
#endif
//queuetp.cpp
#include<iostream>
#include"queuetp.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
void Worker::set()
{
cout << "Enter worker's name : ";
getline(cin, name);
cout << "Enter worker's ID : ";
cin >> id;
while(cin.get() != '\n')
{
continue;
}
}
void Worker::show()const
{
cout << "Name : " << name << " " << "ID : " << id << endl;
}
//main.cpp
#include<iostream>
#include<string>
#include"queuetp.h"
using namespace std;
const int SIZE = 5;
int main()
{
QueueTp<Worker *> wk(SIZE);
Worker *temp;
for(int i = 0; i < SIZE; i++)
{
char ch;
cout << "Enter E(enqueue), D(dequeue), or Q(quit) : ";
cin >> ch;
if((ch == 'Q') || (ch == 'q'))
{
break;
}
switch(ch)
{
case 'E':
case 'e':
cin.get();
temp = new Worker;
temp->set();
if(wk.isfull())
{
cout << "Queue is already full ! " << endl;
}
else
{
wk.enqueue(temp);
cout << "Enqueue successfiily !" << endl;
}
break;
case 'D':
case 'd':
if(wk.isempty())
{
cout << "Queue is already empty ! " << endl;
}
else
{
wk.dequeue(temp);
cout << "Dequue successfiily !" << endl;
}
break;
default:
cout << "Only E(enqueue), D(dequeue), Q(quit) avaible, Please try again !" << endl;
}
}
cout << "The queue counts :" << wk.queueCount() << " items" << endl;
cout << "The queue info as follows : " << endl;
while(!wk.isempty())
{
wk.dequeue(temp);
temp->show();
}
delete temp;
cout << "done . " << endl;
return 0;
}
4:
//persion.h
#ifndef PERSION_H_
#define PERSION_H_
#include<iostream>
#include<string>
using std::string;
class Persion
{
private:
string fname;
string lname;
protected:
virtual void data()const;
virtual void get();
public:
Persion():fname("Null"), lname("Null"){};
Persion(const string &f, const string &l):fname(f), lname(l){};
Persion(const Persion &p):fname(p.fname), lname(p.lname){};
virtual ~Persion() = 0;
virtual void set() = 0;
virtual void show()const = 0;
};
class Gunslinger:virtual public Persion
{
private:
int nicks;
protected:
void data()const;
void get();
public:
Gunslinger():nicks(0), Persion(){};
Gunslinger(int n, const string &f, const string &l):nicks(n), Persion(f, l){};
Gunslinger(int n, const Persion &p):nicks(n), Persion(p){};
void set();
void show()const;
double draw()const;
};
class PokerPlayer:virtual public Persion
{
protected:
void data()const;
public:
PokerPlayer():Persion(){};
PokerPlayer(const string &f, const string &l):Persion(f, l){};
PokerPlayer(const Persion &p):Persion(p){};
void set(){Persion::set(); };
void show()const;
int draw()const;
};
class BadDude:public Gunslinger, public PokerPlayer
{
protected:
void data()const;
void get();
public:
BadDude(){};
BadDude(int n, const string &f, const string &l):Persion(f, l), Gunslinger(n, f, l), PokerPlayer(f, l){};
BadDude(int n, const Persion &p):Persion(p), Gunslinger(n, p), PokerPlayer(p){};
BadDude(const Gunslinger &g):Persion(g), Gunslinger(g), PokerPlayer(g){};
BadDude(int n, const PokerPlayer &pk):Persion(pk), Gunslinger(n, pk), PokerPlayer(pk){};
void set();
void show()const;
int cDraw()const;
double gDraw()const;
};
#endif
//persion.cpp
#include<iostream>
#include<cstdlib>
#include"persion.h"
using std::cout;
using std::endl;
using std::cin;
Persion::~Persion()
{
}
void Persion::data()const
{
cout << "Name info : " << fname << " " << lname << endl;
}
void Persion::get()
{
cout << "Enter the first name : ";
getline(cin, fname);
cout << "Enter the last name : ";
getline(cin, lname);
}
void Persion::show()const
{
data();
}
void Persion::set()
{
get();
}
void Gunslinger::data()const
{
cout << "Nicks info : " << nicks << endl;
cout << "Time for gun getting : " << Gunslinger::draw() << endl;
}
void Gunslinger::get()
{
cout << "Enter nicks : ";
cin >> nicks;
}
void Gunslinger::show()const
{
Persion::data();
data();
}
void Gunslinger::set()
{
Persion::get();
get();
}
double Gunslinger::draw()const
{
return rand() % 3 + 1 ;
}
void PokerPlayer::data()const
{
cout << "Card info : " << draw() << endl;
}
void PokerPlayer::show()const
{
Persion::data();
data();
}
int PokerPlayer::draw()const
{
return rand() % 52 + 1;
}
void BadDude::data()const
{
Gunslinger::data();
PokerPlayer::data();
cout << "the new card info : " << cDraw() << endl;
cout << "the baddude gun getting time : " << gDraw() << endl;
}
void BadDude::get()
{
Gunslinger::get();
}
void BadDude::set()
{
Persion::get();
get();
}
void BadDude::show()const
{
Persion::data();
data();
}
int BadDude::cDraw()const
{
return PokerPlayer::draw();
}
double BadDude::gDraw()const
{
return Gunslinger::draw();
}
//main.cpp
#include<iostream>
#include"persion.h"
using namespace std;
const int SIZE = 5;
int main()
{
Persion *pes[SIZE];
char ch;
for(int i = 0; i < SIZE; i++)
{
cout << "Enter person catelogy: G(Gunslinger), P(PokerPlayer), B(BadDude), or Q(Quit)" << endl;
cin >>ch;
if((ch == 'Q') || (ch == 'q'))
{
break;
}
switch(ch)
{
case 'G':
case 'g':
pes[i] = new Gunslinger;
break;
case 'P':
case 'p':
pes[i] = new PokerPlayer;
break;
case 'B':
case 'b':
pes[i] = new BadDude;
break;
default:
cout << "Only G(Gunslinger), P(PokerPlayer), B(BadDude), Q(quit) is avaiable , Please try again ! " << endl;
}
cin.get();
pes[i]->set();
}
cout << "The Persion Info as follows : " << endl;
for(int i = 0; i < SIZE; i++)
{
pes[i]->show();
cout << endl;
}
for(int i = 0; i < SIZE; i++)
{
delete pes[i];
}
cout << "done . " << endl;
return 0;
}
5:
//emp.h
#ifndef EMP_H_
#define EMP_H_
#include<iostream>
#include<cstring>
using std::string;
using std::ostream;
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;
};
class Employee:public Abstr_emp
{
public:
Employee();
Employee(const string &fn, const string &ln, const string &j);
virtual void showAll()const;
virtual void setAll();
};
class Manger:virtual public Abstr_emp
{
private:
int inchargeof;
protected:
int charge() const{return inchargeof;};
int & charge(){return inchargeof;};
public:
Manger();
Manger(const string &fn, const string &ln, const string &j, int ico = 0);
Manger(const Abstr_emp &e, int ico);
Manger(const Manger &m);
void getCharge();
virtual void showAll()const;
virtual void setAll();
};
class Fink:virtual public Abstr_emp
{
private:
string reportsto;
protected:
const string report()const{return reportsto;};
string & report(){return reportsto;};
public:
Fink();
Fink(const string &fn, const string &ln, const string &j, const string &rpo);
Fink(const Abstr_emp &e, const string &rpo);
Fink(const Fink &f);
void getReport();
virtual void showAll()const;
virtual void setAll();
};
class HighFink:public Manger, public Fink
{
public:
HighFink();
HighFink(const string &fn, const string &ln, const string &j, int ico, const string &rpo);
HighFink(const Abstr_emp &e, int ico, const string &rpo);
HighFink(const Manger &m, const string &rpo);
HighFink(const Fink &f, int ico);
HighFink(const HighFink &h);
virtual void showAll()const;
virtual void setAll();
};
#endif
//emp.cpp
#include<iostream>
#include"emp.h"
using std::cout;
using std::endl;
using std::cin;
using std::string;
Abstr_emp::Abstr_emp():fname("Null"), lname("Null"), job("Null")
{
}
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 << fname << " " << lname << " 's 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 << " 's job : " << e.job << endl;
return os;
}
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();
}
Manger::Manger():Abstr_emp()
{
}
Manger::Manger(const string &fn, const string &ln, const string &j, int ico):inchargeof(ico), Abstr_emp(fn, ln, j)
{
}
Manger::Manger(const Abstr_emp &e, int ico):inchargeof(ico), Abstr_emp(e)
{
}
Manger::Manger(const Manger &m):Abstr_emp(m)
{
}
void Manger::getCharge()
{
cout << "Enter numbers of in charge : ";
cin >> inchargeof;
}
void Manger::showAll()const
{
Abstr_emp::showAll();
cout << "in charge of : " << inchargeof << endl;
}
void Manger::setAll()
{
Abstr_emp::setAll();
getCharge();
}
Fink::Fink():Abstr_emp()
{
}
Fink::Fink(const string &fn, const string &ln, const string &j, const string &rpo):reportsto(rpo), Abstr_emp(fn, ln, j)
{
}
Fink::Fink(const Abstr_emp &e, const string &rpo):reportsto(rpo), Abstr_emp(e)
{
}
Fink::Fink(const Fink &f):Abstr_emp(f)
{
}
void Fink::getReport()
{
cin.get();
cout << "Enter the report story : ";
getline(cin, reportsto);
}
void Fink::showAll()const
{
Abstr_emp::showAll();
cout << "report story : " << reportsto << endl;
}
void Fink::setAll()
{
Abstr_emp::setAll();
getReport();
}
HighFink::HighFink():Abstr_emp(), Manger(), Fink()
{
}
HighFink::HighFink(const string &fn, const string &ln, const string &j, int ico, const string &rpo):Abstr_emp(fn, ln, j), Manger(fn, ln, j, ico), Fink(fn, ln, j, rpo)
{
}
HighFink::HighFink(const Abstr_emp &e, int ico, const string &rpo):Abstr_emp(e), Manger(e, ico), Fink(e, rpo)
{
}
HighFink::HighFink(const Manger &m, const string &rpo):Abstr_emp(m), Manger(m), Fink(m, rpo)
{
}
HighFink::HighFink(const Fink &f, int ico):Abstr_emp(f), Manger(f, ico), Fink(f)
{
}
HighFink::HighFink(const HighFink &h):Abstr_emp(h), Manger(h), Fink(h)
{
}
void HighFink::showAll()const
{
Abstr_emp::showAll();
cout << "in charge of : " << Manger::charge() << endl;
cout << "report story : " << Fink::report() << endl;
}
void HighFink::setAll()
{
Abstr_emp::setAll();
Manger::getCharge();
Fink::getReport();
}
//main.cpp
#include<iostream>
#include"emp.h"
using namespace std;
int main()
{
Employee em("A", "B", "C");
cout << em << endl;
em.showAll();
Manger ma("Am", "Bm", "Cm", 4);
cout << ma << endl;
ma.showAll();
Fink fk("Af", "Bf", "Cf", "Df");
cout << fk << endl;
fk.showAll();
HighFink hk("Ah", "Bh", "Ch", 4, "Dh");
cout << hk << endl;
hk.showAll();
HighFink h1(ma, "Ef");
h1.showAll();
HighFink h2;
h2.setAll();
cout << "Using Abstr_emp pointer : " << endl;
Abstr_emp *tri[4] = {&em, &ma, &fk, &hk};
for(int i = 0; i < 4; i++)
{
tri[i]->showAll();
}
cout << "done . " << endl;
return 0;
}
Practice makes perfect !