728x90
1๋ฒ
//winec.h
#ifndef WINEC_H_
#define WINEC_H_
#include<iostream>
#include<string>
#include<valarray>
template<class T1,class T2>
class Pair
{
private:
T1 year;
T2 num;
public:
T1 first() const { return year; }
T2 second() const { return num; }
Pair() {}
Pair(const T1& y, const T2& n) :year(y),num(n){}
};
class Wine
{
private:
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
PairArray p;
std::string label;
int year_n;
public:
Wine(const char* l, int y, const int yr[], const int bot[]);
Wine(const char* l, int y);
void GetBottles();
const std::string& Label() { return label; }
int sum() const { return p.second().sum(); }
void Show() const;
};
#endif
//winec.cpp
#include"winec.h"
Wine::Wine(const char* l, int y)
{
label = l;
year_n = y;
}
Wine::Wine(const char* l, int y, const int yr[], const int bot[])
:label(l),year_n(y),p(ArrayInt(yr,y),ArrayInt(bot,y))
{
}
void Wine::GetBottles()
{
ArrayInt y(year_n);
ArrayInt n(year_n);
std::cout << year_n << "๋
๊ฐ์ " << label << " ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅํ์์ค:\n";
for (int i = 0; i < year_n; i++)
{
std::cout << "๋
๋๋ฅผ ์
๋ ฅํ์์ค: ";
std::cin >> y[i];
std::cout << "์๋์ ์
๋ ฅํ์์ค: ";
std::cin >> n[i];
}
p=Pair<ArrayInt,ArrayInt>(y, n);
}
void Wine::Show() const
{
std::cout << "์์ธ: " << label << '\n'
<< "\t๋
๋\t์๋\n";
for (int i = 0; i < year_n; i++)
{
std::cout << "\t" << p.first()[i] << "\t" << p.second()[i] << '\n';
}
}
//pe14-1.cpp
#include"winec.h"
int main()
{
using std::cin;
using std::cout;
using std::endl;
cout << "์์ธ ์ด๋ฆ์ ์
๋ ฅํ์์ค: ";
char lab[50];
cin.getline(lab, 50);
cout << "์ํ ๋
๋ ๊ฐ์๋ฅผ ์
๋ ฅํ์์ค: ";
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 << more.Label() << " ์ ์ฒด ์๋"
<< ": " << more.sum() << endl;
cout << "ํ๋ก๊ทธ๋จ ์ข
๋ฃ\n";
return 0;
}
2๋ฒ
//winec.h
#ifndef WINEC_H_
#define WINEC_H_
#include<iostream>
#include<string>
#include<valarray>
template<class T1,class T2>
class Pair
{
private:
T1 year;
T2 num;
public:
T1 first() const { return year; }
T2 second() const { return num; }
Pair() {}
Pair(const T1& y, const T2& n) :year(y),num(n){}
};
class Wine :private std::string,private Pair<std::valarray<int>,std::valarray<int>>
{
private:
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
int year_n;
public:
Wine(const char* l, int y, const int yr[], const int bot[]);
Wine(const char* l, int y);
void GetBottles();
const std::string& Label() { return (const std::string &) *this; }
int sum() const { return PairArray::second().sum(); }
void Show() const;
};
#endif
//winec.cpp
#include"winec.h"
Wine::Wine(const char* l, int y) : std::string(l),year_n(y)
{
}
Wine::Wine(const char* l, int y, const int yr[], const int bot[])
:std::string(l),year_n(y),PairArray(ArrayInt(yr,y),ArrayInt(bot,y))
{
}
void Wine::GetBottles()
{
ArrayInt y(year_n);
ArrayInt n(year_n);
std::cout << year_n << "๋
๊ฐ์ " << (const std::string &)*this << " ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅํ์์ค:\n";
for (int i = 0; i < year_n; i++)
{
std::cout << "๋
๋๋ฅผ ์
๋ ฅํ์์ค: ";
std::cin >> y[i];
std::cout << "์๋์ ์
๋ ฅํ์์ค: ";
std::cin >> n[i];
}
(PairArray &)*this=PairArray(y, n);
}
void Wine::Show() const
{
std::cout << "์์ธ: " << (const std::string&) * this << '\n'
<< "\t๋
๋\t์๋\n";
for (int i = 0; i < year_n; i++)
{
std::cout << "\t" << PairArray::first()[i] << "\t" << PairArray::second()[i] << '\n';
}
}
3๋ฒ
//QueueTp.h
#ifndef QUEUETP_H_
#define QUEUETP_H_
template<class T>
class Queue
{
private:
// ํด๋์ค ์ฌ์ฉ ๋ฒ์์ ์ ์๋ค
// Node๋ ์ด ํด๋์ค์ ์ง์ญ์ ์ธ, ๋ดํฌ๋ ๊ตฌ์กฐ์ฒด ์ ์์ด๋ค
struct Node { T item; struct Node* next; };
enum { Q_SIZE = 3 };
// private ํด๋์ค ๋ฉค๋ฒ๋ค
Node* front; // Queue์ ๋จธ๋ฆฌ๋ฅผ ์ง์ํ๋ ํฌ์ธํฐ
Node* rear; // Queue์ ๊ผฌ๋ฆฌ๋ฅผ ์ง์ํ๋ ํฌ์ธํฐ
int items; // Queue์ ์๋ ํ์ฌ ํญ๋ชฉ ์
const int qsize; // Queue์ ๋ฃ์ ์ ์๋ ์ต๋ ํญ๋ชฉ ์
// public ๋ณต์ฌ๋ฅผ ๋ฐฉ์งํ๋ ์ ์ ์ ์
Queue(const Queue& q) : qsize(0) { }
public:
Queue(int qs = Q_SIZE); // qs ํ๊ณ๋ฅผ ๊ฐ์ง ํ๋ฅผ ์์ฑํ๋ค
~Queue();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const T& item); // ํญ๋ชฉ์ ๊ผฌ๋ฆฌ์ ์ถ๊ฐํ๋ค
bool dequeue(T& item); // ๋จธ๋ฆฌ์์ ํญ๋ชฉ์ ์ญ์ ํ๋ค
Queue& operator=(const Queue& q) { return *this; }
};
template<class T>
Queue<T>::Queue(int qs) : qsize(qs)
{
front = rear = NULL; // ๋๋ nullptr
items = 0;
}
template<class T>
Queue<T>::~Queue()
{
Node* temp;
while (front != NULL) // ํ๊ฐ ์์ง ๋น์ด ์์ง ์์ผ๋ฉด
{
temp = front; // ๋จธ๋ฆฌ ํญ๋ชฉ์ ์ฃผ์๋ฅผ ์์๋ก ์ ์ฅํ๋ค
front = front->next; // front๋ฅผ ๊ทธ ๋ค์ ํญ๋ชฉ์ผ๋ก ๋ค์ ์ค์ ํ๋ค
delete temp; // ์ด์ ์ ๋จธ๋ฆฌ ๋
ธ๋๋ฅผ ์ญ์ ํ๋ค
}
}
template<class T>
bool Queue<T>::isempty() const
{
return items == 0;
}
template<class T>
bool Queue<T>::isfull() const
{
return items == qsize;
}
template<class T>
int Queue<T>::queuecount() const
{
return items;
}
// ํ์ ํญ๋ชฉ์ ์ถ๊ฐํ๋ค
template<class T>
bool Queue<T>::enqueue(const T& item)
{
if (isfull())
return false;
Node* add = new Node; // ๋
ธ๋๋ฅผ ์์ฑํ๋ค
// ์คํจํ๋ฉด, new๋ std::bad_alloc exception๋ฅผ ๋ฐ์์ํจ๋ค
add->item = item; // ํ๊ฐ ๋น์ด ์์ผ๋ฉด
add->next = NULL; // ๋๋ nullptr;
items++;
if (front == NULL) // ํ๊ฐ ๋น์ด ์์ผ๋ฉด
front = add; // ํญ๋ชฉ์ ๋จธ๋ฆฌ์ ๋ฃ๋๋ค
else
rear->next = add; // ๊ทธ๋ ์ง ์์ผ๋ฉด ๊ผฌ๋ฆฌ์ ๋ฃ๋๋ค
rear = add; // rear๊ฐ ์ ๋
ธ๋๋ฅผ ์ง์ํ๊ฒ ๋ง๋ ๋ค
return true;
}
// ๋จธ๋ฆฌ ํญ๋ชฉ์ item ๋ณ์์ ๋ฃ๊ณ ํ์์ ์ญ์ ํ๋ค
template<class T>
bool Queue<T>::dequeue(T& item)
{
if (front == NULL)
return false;
item = front->item; // ํ์ ๋จธ๋ฆฌ ํญ๋ชฉ์ item์ ๋ณต์ฌํ๋ค
items--;
Node* temp = front; // ๋จธ๋ฆฌ ํญ๋ชฉ์ ์์น๋ฅผ ์์๋ก ์ ์ฅํ๋ค
front = front->next; // front๋ฅผ ๊ทธ ๋ค์ ํญ๋ชฉ์ผ๋ก ๋ค์ ์ค์ ํ๋ค
delete temp; // ์ด์ ์ ๋จธ๋ฆฌ ํญ๋ชฉ์ ์ญ์ ํ๋ค
if (items == 0)
rear = NULL;
return true;
}
#endif
// workermi.h -- ๋ค์ค ์์์ ์ฌ์ฉํ๋ ๋
ธ๋ฌด ํด๋์ค๋ค
#ifndef WORKERMI_H_
#define WORKERMI_H_
#include <string>
class Worker // ์ถ์ํ ๊ธฐ์ด ํด๋์ค
{
private:
std::string fullname;
long id;
protected:
virtual void Data() const;
virtual void Get();
public:
Worker() : fullname("no name"), 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 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;
};
#endif
// workermi.cpp -- ๋ค์ค ์์์ ์ฌ์ฉํ๋ working ํด๋์ค ๋ฉ์๋๋ค
#include "workermi.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
// Worker ๋ฉ์๋๋ค
Worker::~Worker() { }
// protected ๋ฉ์๋๋ค
void Worker::Data() const
{
cout << "์ฌ์ ์ด๋ฆ: " << fullname << endl;
cout << "์ฌ์ ๋ฒํธ: " << id << endl;
}
void Worker::Get()
{
getline(cin, fullname);
cout << "์ฌ์ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ญ์์ค: ";
cin >> id;
while (cin.get() != '\n')
continue;
}
// Waiter ๋ฉ์๋๋ค
void Waiter::Set()
{
cout << "์จ์ดํฐ์ ์ด๋ฆ์ ์
๋ ฅํ์ญ์์ค: ";
Worker::Get();
Get();
}
void Waiter::Show() const
{
cout << "์ง์ข
: ์จ์ดํฐ\n";
Worker::Data();
Data();
}
// protected ๋ฉ์๋๋ค
void Waiter::Data() const
{
cout << "์จ์ดํฐ ๋ฑ๊ธ: " << panache << endl;
}
void Waiter::Get()
{
cout << "์จ์ดํฐ ๋ฑ๊ธ์ ์
๋ ฅํ์ญ์์ค: ";
cin >> panache;
while (cin.get() != '\n')
continue;
}
// Singer ๋ฉ์๋๋ค
char* Singer::pv[Singer::Vtypes] = { "other", "alto", "contralto",
"soprano", "bass", "baritone", "tenor" };
void Singer::Set()
{
cout << "๊ฐ์์ ์ด๋ฆ์ ์
๋ ฅํ์ญ์์ค: ";
Worker::Get();
Get();
}
void Singer::Show() const
{
cout << "์ง์ข
: ๊ฐ์\n";
Worker::Data();
Data();
}
// protected ๋ฉ์๋๋ค
void Singer::Data() const
{
cout << "๋ชฉ์๋ฆฌ ์ ํ: " << pv[voice] << endl;
}
void Singer::Get()
{
cout << "๊ฐ์์ ๋ชฉ์๋ฆฌ ์ ํ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ญ์์ค:\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;
}
// SingingWaiter ๋ฉ์๋๋ค
void SingingWaiter::Data() const
{
Singer::Data();
Waiter::Data();
}
void SingingWaiter::Get()
{
Waiter::Get();
Singer::Get();
}
void SingingWaiter::Set()
{
cout << "๊ฐ์ ๊ฒธ ์จ์ดํฐ์ ์ด๋ฆ์ ์
๋ ฅํ์ญ์์ค: ";
Worker::Get();
Get();
}
void SingingWaiter::Show() const
{
cout << "์ง์ข
: ๊ฐ์ ๊ฒธ ์จ์ดํฐ\n";
Worker::Data();
Data();
}
// pe14-3.cpp -- ๋ค์ค ์์
// workermi.cpp์ ํจ๊ป ์ปดํ์ผํ๋ค
#include <iostream>
#include <cstring>
#include "workermi.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];
Worker* temp=nullptr;
char ch;
cout << "ํ๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด A, ํ๋ฅผ ์ฒ๋ฆฌํ๋ ค๋ฉด P,\n ์ข
๋ฃํ๋ ค๋ฉด Q๋ฅผ ์
๋ ฅํ์ญ์์ค.\n";
while (cin >> ch && std::toupper(ch) != 'Q')
{
while (cin.get() != '\n')
continue;
switch (ch)
{
case 'A':
case 'a':
int ct;
for (ct = 0; ct < SIZE; ct++)
{
lolas[ct] = new Queue<Worker*>;
cout << ct + 1 << "๋ฒ์งธ queue๋ฅผ ์ ๋ถ ์ถ๊ฐ\n";
for (int i = 0; i < 3; i++)
{
char choice;
cout << "์ง์ข
์ ์
๋ ฅํ์ญ์์ค:\n"
<< "w: ์จ์ดํฐ s: ๊ฐ์ "
<< "t: ๊ฐ์ ๊ฒธ ์จ์ดํฐ q: ์ข
๋ฃ\n";
cin >> choice;
while (strchr("wstq", choice) == NULL)
{
cout << "w, s, t, q ์ค์์ ํ๋๋ฅผ ์ ํํ์ญ์์ค: ";
cin >> choice;
}
if (choice == 'q')
break;
switch (choice)
{
case 'w':
temp = new Waiter;
break;
case 's':
temp = new Singer;
break;
case 't':
temp = new SingingWaiter;
break;
}
cin.get();
temp->Set();
lolas[ct]->enqueue(temp); //ct๊ฐ 0 ์ผ๋ lolas[0]์์ ์๋ ์ฒซ๋ฒ์งธ q๊ฐ temp๋ก ์ด๊ธฐํ
cout << "----------------------------\n";
temp->Show();
cout << "๊ฐ ์ถ๊ฐ๋จ\n----------------------------\n";
}
}//ํ๊ฐ ๊ฐ๋์ฐฐ๋ ๊น์ง ์งํ
break;
case 'P':
case 'p':
for (ct = 0; ct < SIZE; ct++)
{
cout << ct + 1 << "๋ฒ์งธ์ ๋ชจ๋ ํ ์ ๊ฑฐ\n";
for (int i = 0; i < 3; i++)
{
lolas[ct]->dequeue(temp);
cout << "----------------------------\n";
temp->Show();
cout << "๊ฐ ์ ๊ฑฐ๋จ\n----------------------------\n";
}
}
break;
}
cout << "ํ๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด A, ํ๋ฅผ ์ฒ๋ฆฌํ๋ ค๋ฉด P,\n ์ข
๋ฃํ๋ ค๋ฉด Q๋ฅผ ์
๋ ฅํ์ญ์์ค.\n";
}
delete temp;
cout << "ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n";
return 0;
}
4๋ฒ
//person.h
#ifndef PERSON_H_
#define PERSON_H_
#include<iostream>
#include<string>
#include<time.h>
#include<stdlib.h>
class Person
{
private:
std::string pname;
std::string lname;
protected:
virtual void Get();
virtual void Data() const;
public:
Person(const std::string p,const std::string l) :pname(p),lname(l){}
Person() : pname("none"),lname("none") {}
virtual ~Person() = 0;
virtual void Set() = 0;
virtual void Show() const=0;
};
class Gunslinger : virtual public Person
{
private:
double g_time;
int notch;
protected:
void Get();
void Data() const;
public:
Gunslinger(const std::string p,const std::string l,double g = 0,int n = 0)
:Person(p,l),g_time(g),notch(n) {}
Gunslinger() : Person(),g_time(0),notch(0) {}
Gunslinger(const Person& p, double g, int n)
:Person(p), g_time(g), notch(n) {}
double Draw() const{ return g_time; }
void Show() const;
void Set();
};
class PokerPlayer :virtual public Person
{
protected:
void Data() const;
public:
PokerPlayer(const std::string p, const std::string l)
:Person(p, l) {}
PokerPlayer(const Person &p)
:Person(p) {}
PokerPlayer():Person(){}
int Draw() const { return (rand() % 52) + 1; }
void Show() const;
void Set();
};
class BadDude : public Gunslinger,public PokerPlayer
{
protected:
virtual void Get();
virtual void Data() const;
public:
BadDude() {}
BadDude(const std::string p,const std::string l,double g=0,int n=0)
: Person(p,l),Gunslinger(p,l,g,n) {}
BadDude(const Person &p,double g=0,int n=0)
:Person(p),Gunslinger(p,g,n) {}
BadDude(const Gunslinger &g)
:Person(g),Gunslinger(g) {}
double gdraw() const { return Gunslinger::Draw(); }
int cdraw() const { return PokerPlayer::Draw(); }
void Set();
void Show() const;
};
#endif
//person.cpp
#include"person.h"
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
Person::~Person() {}
void Person::Get()
{
cout << "์ฑ ์
๋ ฅ: ";
getline(cin, lname);
cout << "์ด๋ฆ ์
๋ ฅ : ";
getline(cin, pname);
}
void Person::Data() const
{
cout << "์ด๋ฆ : " << pname << ", " << lname << endl;
}
void Gunslinger::Set()
{
cout << "์ด์ก์ด์ ์ด๋ฆ ์
๋ ฅ:\n";
Person::Get();
Get();
}
void Gunslinger::Show() const
{
cout << "์ง์
: ์ด์ก์ด\n";
Person::Data();
Data();
}
void Gunslinger::Data() const
{
cout << "๊ถ์ด์ ๋นผ๋ด๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ : " << Draw() << endl;
cout << "๊ถ์ด์ ์๊ฒจ์ง ๊ธ์ : " << notch << endl;
}
void Gunslinger::Get()
{
cout << "๊ถ์ด์ ๋นผ๋ด๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ ์
๋ ฅ : ";
cin >> g_time;
cout << "๊ถ์ด์ ์๊ฒจ์ง ๊ธ์ ์
๋ ฅ : ";
cin >> notch;
while (cin.get() != '\n')
continue;
}
void PokerPlayer::Set()
{
cout << "PokerPlayer์ ์ด๋ฆ ์
๋ ฅ: \n";
PokerPlayer::Get();
}
void PokerPlayer::Show() const
{
cout << "์ง์
: PokerPlayer\n";
Person::Data();
Data();
}
void PokerPlayer::Data() const
{
cout << "๋ค์์ ๊บผ๋ผ ์นด๋ ๊ฐ : " << Draw() << endl;
}
void BadDude::Data() const
{
Gunslinger::Data();
PokerPlayer::Data();
}
void BadDude::Get()
{
Gunslinger::Get();
}
void BadDude::Set()
{
cout << "์ด์ก์ด ๊ฒธ Pokerplayer์ ์ด๋ฆ ์
๋ ฅ:\n";
Person::Get();
Get();
}
void BadDude::Show() const
{
cout << "์ง์
: ์ด์ก์ด, PokerPlayer\n";
Person::Data();
Data();
}
//pe14-4.cpp
#include <iostream>
#include <cstring>
#include "person.h"
const int SIZE = 5;
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::strchr;
Person* lolas[SIZE];
int ct;
for (ct = 0; ct < SIZE; ct++)
{
char choice;
cout << "์ง์ข
์ ์
๋ ฅํ์ญ์์ค:\n"
<< "g: ์ด์ก์ด p: Pokerplayer "
<< "b: ์ด์ก์ด ๊ฒธ Pokerplayer q: ์ข
๋ฃ\n";
cin >> choice;
while (strchr("gpbq", choice) == NULL)
{
cout << "g, p, b, q ์ค์์ ํ๋๋ฅผ ์ ํํ์ญ์์ค: ";
cin >> choice;
}
if (choice == 'q')
break;
switch (choice)
{
case 'g': lolas[ct] = new Gunslinger;
break;
case 'p': lolas[ct] = new PokerPlayer;
break;
case 'b': lolas[ct] = new BadDude;
break;
}
cin.get();
lolas[ct]->Set();
}
cout << "\n์ฌ์ ํํฉ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:\n";
int i;
for (i = 0; i < ct; i++)
{
cout << endl;
lolas[i]->Show();
}
for (i = 0; i < ct; i++)
delete lolas[i];
cout << "ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n";
return 0;
}
5๋ฒ
//emp.h
#include<iostream>
#include<string>
class abstr_emp
{
private:
std::string fname; //abstr_emp์ ํผ์คํธ ๋ค์
std::string lname; //abstr_emp์ ๋ผ์คํธ ๋ค์
std::string job; //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;
};
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& rpo);
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"emp.h"
using std::cout;
using std::endl;
using std::cin;
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 <<"์ด๋ฆ : " <<fname << ", " << lname << endl;
cout << "์ง์
: " << job << endl;
}
void abstr_emp::SetAll() //์ฌ์ฉ์์๊ฒ ๊ฐ๋ค์ ์
๋ ฅํ๋ผ๊ณ ์๊ตฌํ๋ค.
{
cout << "ํผ์คํธ ๋ค์ ์
๋ ฅ : "; getline(cin,fname);
cout << "๋ผ์คํธ ๋ค์ ์
๋ ฅ : "; getline(cin, lname);
cout << "์ง์
์
๋ ฅ : "; 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::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) {
inchargeof = m.inchargeof;
}
void manager::ShowAll() const
{
abstr_emp::ShowAll();
cout << "inchargeof : " << inchargeof << endl;
}
void manager::SetAll()
{
abstr_emp::SetAll();
cout << "inchargeof ์
๋ ฅ : "; cin >> inchargeof;
while (cin.get() != '\n')
continue;
}
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 << "reportsto ์
๋ ฅ : "; cin >> reportsto;
while (cin.get() != '\n')
continue;
}
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 << "inchargeof : " << manager::InChargeOf() << endl;
cout << "reportsto : " << fink::ReportsTo() << endl;
}
void highfink::SetAll()
{
abstr_emp::SetAll();
cout << "inchargeof ์
๋ ฅ : "; cin >> manager::InChargeOf();
cin.get();
cout << "reportsto ์
๋ ฅ : "; getline(cin, fink::ReportsTo());
}
//pe14-5.cpp
#include<iostream>
using namespace std;
#include "emp.h"
int main(void)
{
employee em("Trip", "Harris", "Thumper");
cout << em << endl;
em.ShowAll();
manager ma("Amorphia", "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 << "๋ค์ ์ถ๋ ฅ์ ์ํด ์๋ฌด ํค๋ ๋๋ฅด์ญ์์ค:\n";
cin.get();
highfink hf2;
hf2.SetAll();
cout << "abstr_emp * ํฌ์ธํฐ์ ์ฌ์ฉ:\n";
abstr_emp* tri[4] = { &em,&fi,&hf,&hf2 };
for (int i = 0; i < 4; i++)
tri[i]->ShowAll();
return 0;
}
/*๋์
์ฐ์ฐ์๋ฅผ ์ ์ํ์ง ์์ ์ด์
abstr_emp * ์ ํด๋์ค์ ๋์
์ด ์๋ ํฌ์ธํฐ์ ๋์
์ด๋ฏ๋ก ํ์์๋ค.
ShowAll()๊ณผ SetAll์ ํด๋์ค๋ง๋ค ์ ์๊ฐ ๋ค๋ฅด๋ฏ๋ก ๊ฐ์
abstr_emp๋ฅผ ๊ฐ์ ๊ธฐ์ด ํด๋์ค๋ก ์ ์ธํ์ง ์์ผ๋ฉด highfink์์ manager์ fink์ ์ํด abstr_emp ํด๋์ค๋ฅผ ์ค๋ณต์ผ๋ก ๊ฐ์ง๊ฒ๋จ.
highfink ํด๋์ค์ ๋ฐ์ดํฐ ๋ถ๋ถ์ manager์ fink๊ฐ ๊ฐ์ง๊ณ ์๋ค.
operator<<()๊ฐ ํ๊ฐ์ง ๋ฒ์ ๋ง ํ์ํ ์ด์ ๋ , ํผ์คํธ๋ค์๊ณผ ๋ผ์คํธ๋ค์๋ง ์ถ๋ ฅํ๊ณ , ํ๋ ๋ ํจ์์ด๊ธฐ ๋๋ฌธ
ํ๋ก๊ทธ๋จ ๋ ๋ถ๋ถ ์ฝ๋๋ฅผ ๋ค์์ผ๋ก ๋์ฒดํ๋ฉด ... ๋์
์ฐ์ฐ์๊ฐ ์๋ค๊ณ ๋์ฌ๊ฒ์ด๋ค. */
728x90