hugDog
Android DevLog
hugDog
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๐Ÿ™Œ Hello? (162)
    • ๐Ÿงฉ์•ˆ๋“œ๋กœ์ด๋“œ (12)
      • ๊ฐœ๋… ์ •๋ฆฌ (5)
      • ๋ฒ„๊ทธ ํ•ด๊ฒฐ (4)
      • ๊ธฐํƒ€ (3)
    • ๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜ (54)
      • ๊ฐœ๋… (0)
      • ๋ฐฑ์ค€ (48)
      • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (6)
    • ๐Ÿ“„๊ฐœ๋ฐœ ์ผ์ง€ (0)
      • FINPO (0)
    • ๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด (71)
      • C++ ์ •๋ฆฌ (49)
      • C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ (20)
      • Kotlin (2)
    • โญProject (1)
    • ๐ŸšดTIL (13)
      • Clean Code (13)
    • ๐Ÿšฉ๊ธฐํƒ€ (9)
      • ๋ชฉํ‘œ (6)
      • ์ผ์ƒ (3)
      • ๋ฌธ์„œ (0)

์ธ๊ธฐ ๊ธ€

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
hugDog

Android DevLog

๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ

(C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 14 C++ ์ฝ”๋“œ์˜ ์žฌํ™œ์šฉ p.1103~ 1๋ฒˆ~5๋ฒˆ

2020. 10. 3. 16:08
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
์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ (์ƒˆ์ฐฝ์—ด๋ฆผ)
    '๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 16 string ํด๋ž˜์Šค์™€ ํ‘œ์ค€ ํ…œํ”Œ๋ฆฟ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ p.1342~ 1๋ฒˆ~10๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 15 ํ”„๋ Œ๋“œ,์˜ˆ์™ธ,๊ธฐํƒ€ ์‚ฌํ•ญ p.1206~ 1๋ฒˆ~4๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 13 ํด๋ž˜์Šค์˜ ์ƒ์† p.989~ 1๋ฒˆ~4๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 12 ํด๋ž˜์Šค์™€ ๋™์  ๋ฉ”๋ชจ๋ฆฌ ๋Œ€์ž… p.892~ 1๋ฒˆ~6๋ฒˆ
    hugDog
    hugDog
    ์•ˆ๋“œ๋กœ์ด๋“œ ๊ณต๋ถ€ ์ค‘์ธ ํ•™์ƒ์ž…๋‹ˆ๋‹ค!

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”