🌈λͺ…ν’ˆ C++ ν”„λ‘œκ·Έλž˜λ° 8μž₯πŸ§‘β€πŸ’»

Se0ng_1lΒ·2022λ…„ 6μ›” 26일
0
post-thumbnail

8μž₯ μš”μ 

상속

상속

C++의 클래슀 상속

λΆ€λͺ¨ 클래슀의 멀버듀을 μžμ‹ ν΄λž˜μŠ€μ— λ¬Όλ €λ°›κ²Œ ν•˜μ—¬ μžμ‹ ν΄λž˜μŠ€κ°€ λΆ€λͺ¨ 클래슀의 멀버에 접근이 κ°€λŠ₯ν•˜λ„λ‘ ν•˜λŠ” 것
λΆ€λͺ¨μ˜ μœ μ „μžλ₯Ό 물렀받은 κ°œλ…

⚠️ μžμ‹μ΄ λΆ€λͺ¨μ˜ μœ μ „μžλŠ” λ¬Όλ €λ°›μ•˜μ§€λ§Œ, μžμ‹μ˜ 비밀은
λͺ¨λ₯΄κΈ° λ•Œλ¬Έμ— λΆ€λͺ¨ν΄λž˜μŠ€λŠ” μžμ‹ν΄λž˜μŠ€μ˜ λ©€λ²„μ—λŠ” μ ‘κ·Όν•  수 μ—†λ‹€.

μ„ μ–Έ

class Subject{ // λΆ€λͺ¨ 클래슀
};
class Korean : public Subject{ // μžμ‹ 클래슀
    // μ ‘κ·Όμ§€μ •μžλŠ” private, protected도 κ°€λŠ₯ν•˜λ‹€.
};

μ‚¬μš©

상속을 톡해 λΆ€λͺ¨ 클래슀의 멀버가 μžμ‹ 클래슀의 λ©€λ²„λ‘œ ν™•μž₯λœλ‹€.
ex) λΆ€λͺ¨ν΄λž˜μŠ€(Subject)에 getPrint(); ν•¨μˆ˜κ°€ μžˆλŠ” κ°€μ •

int main(){
	Korean k;
    k.getPrint();
}    

βš οΈλΆ€λͺ¨ν΄λž˜μŠ€μ˜ privateλ©€λ²„λŠ” μžμ‹ 클래슀의 λ©€λ²„ν•¨μˆ˜λ‘œ 접근은 λΆˆκ°€λŠ₯ν•˜λ‹€.
βš οΈμ ‘κ·Όμ„ μœ„ν•΄μ„œλŠ” λΆ€λͺ¨ ν΄λž˜μŠ€μ— 접근을 μœ„ν•œ ν•¨μˆ˜λ₯Ό κ΅¬ν˜„ν•΄μ•Ό ν•œλ‹€.
publicλ©€λ²„λŠ” μ–΄λ””μ„œλ“  μžμ‹ ν΄λž˜μŠ€κ°€ 접근이 κ°€λŠ₯ν•˜λ‹€.

상속과 객체 포인터

μ—… μΊμŠ€νŒ…

μžμ‹ 클래슀의 객체λ₯Ό λΆ€λͺ¨ 클래슀의 ν¬μΈν„°λ‘œ κ°€λ¦¬ν‚€λŠ” 것
μžμ‹ 클래슀의 객체λ₯Ό λΆ€λͺ¨ 클래슀의 객체처럼 λ‹€λ£° 수 있게 ν•œλ‹€.

	Korean k;
    Korean *pK = &k;
    Subject *pBase = pK; // μ—…μΊμŠ€νŒ… 
    // μ›λž˜ ν˜•νƒœ = Subject *pBase = (Subject*)pK;
    // (Subject*)λŠ” μƒλž΅μ΄ κ°€λŠ₯ν•˜λ‹€.

βš οΈν•˜μ§€λ§Œ, μžμ‹ 클래슀처럼 λ³΄μ΄λŠ” λΆ€λͺ¨ν΄λž˜μŠ€ μ΄λ―€λ‘œ μžμ‹ 클래슀의 λ©€λ²„μ—λŠ” 접근이 λΆˆκ°€λŠ₯ν•˜λ‹€.

λ‹€μš΄ μΊμŠ€νŒ…

λΆ€λͺ¨ 클래슀 포인터가 κ°€λ¦¬ν‚€λŠ” 객체λ₯Ό μžμ‹ 클래슀의 ν¬μΈν„°λ‘œ
κ°€λ¦¬ν‚€λŠ” 것

	Korean k;
    Korean *pK = &k;
    Subject *pSub = pK; // μ—…μΊμŠ€νŒ… 
    pK = (Subject *)pSub; // λ‹€μš΄ μΊμŠ€νŒ…
    // λ‹€μš΄ μΊμŠ€νŒ…μ€ (Subject*)λ₯Ό μƒλž΅ν•΄μ„œλŠ” μ•ˆλ˜κ³  κΌ­ λͺ…μ‹œν•΄μ•Όν•œλ‹€.
    // κ°•μ œλ‘œ νƒ€μž…μ„ λ°”κΎΈλŠ” κ°œλ…μ΄κΈ° λ•Œλ¬Έμ΄λ‹€.

βš οΈλ‹€μš΄ μΊμŠ€νŒ…μ˜ 경우, λΆ€λͺ¨ 클래슀의 멀버에 접근이 κ°€λŠ₯ν•˜λ‹€.
(μ ‘κ·Όμ§€μ •μžμ— 따라 달리진닀.)

protected

❗️privateκ³Ό λ‹€λ₯΄κ²Œ
protected둜 μ§€μ •λœ λ©€λ²„λŠ” νŒŒμƒ ν΄λž˜μŠ€μ—κ²Œ 접근을 ν—ˆμš©ν•œλ‹€.
❗️publicκ³Ό λ‹€λ₯΄κ²Œ
μ™ΈλΆ€μ—μ„œμ˜ 접근은 ν—ˆμš©ν•˜μ§€ μ•ŠλŠ”λ‹€.

상속과 μƒμ„±μž, μ†Œλ©Έμž

μžμ‹ 클래슀 객체 μƒμ„±μ‹œ λΆ€λͺ¨ ν΄λž˜μŠ€μ™€ μžμ‹ 클래슀의 μƒμ„±μž λ‘˜λ‹€ μ‹€ν–‰λ˜κ³  μ†Œλ©Έμž λ˜ν•œ λ‘˜λ‹€ μ‹€ν–‰λœλ‹€.
β—οΈμˆœμ„œ
μƒμ„±μž(λΆ€λͺ¨) -> μƒμ„±μž(μžμ‹) -> μ†Œλ©Έμž(μžμ‹) -> μ†Œλ©Έμž(λΆ€λͺ¨)

❓ 그러면 객체 μƒμ„±μ‹œ λ§€κ°œλ³€μˆ˜ μ²˜λ¦¬λŠ” μ–΄λ–»κ²Œ ν• κΉŒ?
1. μžμ‹, λΆ€λͺ¨ λ‘˜λ‹€ μƒμ„±μž ν•¨μˆ˜λ₯Ό λ§Œλ“€μ§€ μ•Šμ€ 경우
=> λ‘˜λ‹€ μ•”λ¬΅μ μœΌλ‘œ κΈ°λ³Έ μƒμ„±μž ν•¨μˆ˜κ°€ ν˜ΈμΆœλœλ‹€.
2. μžμ‹ μƒμ„±μžλŠ” 기본적으둜 λΆ€λͺ¨μ˜ κΈ°λ³Έ μƒμ„±μž ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œλ‹€.
=> λ§Œμ•½, λΆ€λͺ¨ μƒμ„±μžμ— κΈ°λ³Έ μƒμ„±μž 외에 λ‹€λ₯Έ μƒμ„±μžκ°€ 있으면 λ‹€λ₯Έ μƒμ„±μžλŠ” ν˜ΈμΆœλ˜μ§€ μ•ŠλŠ”λ‹€.
⚠️ λΆ€λͺ¨ ν΄λž˜μŠ€μ— κΈ°λ³Έ μƒμ„±μžκ°€ μ—†κ³ , λ‹€λ₯Έ μƒμ„±μžλ§Œ μžˆλ‹€λ©΄ 였λ₯˜ λ°œμƒ
3. μžμ‹ μƒμ„±μžκ°€ λΆ€λͺ¨ μƒμ„±μž 쀑 κΈ°λ³Έ μƒμ„±μžκ°€ μ•„λ‹Œ λ‹€λ₯Έ μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜κ³  μ‹Άλ‹€λ©΄ λͺ…μ‹œν•΄μ€˜μ•Όν•œλ‹€.

ex) Korean(int sum, int student) : Subject(sum / student){}

public, private, protected 상속

상속할 λ•Œ, λΆ€λͺ¨ν΄λž˜μŠ€μ˜ μ ‘κ·Όμ§€μ •μž
public => λΆ€λͺ¨ν΄λž˜μŠ€μ˜ protected, public은 μ ‘κ·Ό 지정 λ³€κ²½ 없이 μžμ‹ ν΄λž˜μŠ€μ— κ·ΈλŒ€λ‘œ 상속
private => λΆ€λͺ¨ν΄λž˜μŠ€μ˜ protected, public은 μ ‘κ·Ό 지정이 private이 λ˜μ–΄ μžμ‹ ν΄λž˜μŠ€μ— 상속
protected => λΆ€λͺ¨ν΄λž˜μŠ€μ˜ protected, public은 μ ‘κ·Ό 지정이 protected이 λ˜μ–΄ μžμ‹ ν΄λž˜μŠ€μ— 상속
⚠️ μ ‘κ·Όμ§€μ •μž μƒλž΅μ‹œ μžλ™μœΌλ‘œ private이 λœλ‹€.

닀쀑 상속

μžμ‹μ€ μ—„λ§ˆ, μ•„λΉ μ˜ μœ μ „μžλ₯Ό λͺ¨λ‘ λ¬Όλ €λ°›λŠ” κ°œλ…
ν΄λž˜μŠ€λ„ μ—¬λŸ¬ λΆ€λͺ¨ 클래슀λ₯Ό 상속 받을 수 μžˆλ‹€.
μ„ μ–Έ

class Child : public Mom, public Daddy{};

가상 상속

βš οΈλ‹€μ€‘ μƒμ†μ˜ 문제점 : 쀑볡 μƒμ†μœΌλ‘œ μΈν•œ λͺ¨ν˜Έμ„± 문제 λ°œμƒ
이 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ λ„μž…λœ 것이 가상 상속이닀.
μ»΄νŒŒμΌλŸ¬μ—κ²Œ μžμ‹ν΄λž˜μŠ€μ˜ 객체가 생성될 λ•Œ κΈ°λ³Έ 클래슀의 멀버 곡간을 였직 ν•œ 번만 ν• λ‹Ήν•œλ‹€.
이미 ν• λ‹Ήλ˜μ–΄ μžˆλ‹€λ©΄ ν• λ‹Ήλœ 곡간을 κ³΅μœ ν•˜λ„λ‘ μ§€μ‹œν•œλ‹€.
클래슀 상속이 닀이아λͺ¬λ“œ ꡬ쑰일 λ•Œ 주둜 λ°œμƒ

κ·Έλ¦Ό μ„€λͺ… (이런 λŠλ‚ŒπŸ™„)

λͺ…ν’ˆ C++ν”„λ‘œκ·Έλž˜λ° μ±…
https://book.naver.com/bookdb/book_detail.naver?bid=13395206

⚠️ 주의 ⚠️
1. μ—¬κΈ°μ„œλΆ€ν„°λŠ” μ‹€μŠ΅λ¬Έμ œ μ •λ‹΅μž…λ‹ˆλ‹€.
2.μ œκ°€ μž‘μ„±ν•œ 정닡이 틀릴 수 μžˆμŠ΅λ‹ˆλ‹€. ν˜Ήμ‹œ ν‹€λ¦°κ²Œ μžˆλ‹€λ©΄ λŒ“κΈ€λ‘œ λ‚¨κ²¨μ£Όμ‹œλ©΄ κ°μ‚¬νžˆ μˆ˜μ •ν•˜κ² μŠ΅λ‹ˆλ‹€.
3. C++λ₯Ό κ³΅λΆ€ν•˜μ‹œλŠ” ν•™μƒμ΄μ‹œλΌλ©΄ λ³΄μ‹œκΈ° 전에 직접 κΌ­ ν•œ 번 ν’€μ–΄λ³΄μ„Έμš”!

μ‹€μŠ΅ 문제 8 - 1, 2 (Circle 클래슀)
문제 : 1
Circle클래슀λ₯Ό 상속받은 NamedCircle클래슀 μ™„μ„±ν•˜κΈ°

#include <iostream>
#include <string>
using namespace std;

class Circle{
    int radius;
public:
    Circle(int radius = 0) {this->radius = radius;}
    int getRadius() { return radius;}
    void setRadius(int radius) {this->radius = radius;}
    double getArea() {return 3.14 * radius * radius;}
};

class NamedCircle:public Circle{
    string name;
public:
    NamedCircle(int radius, string name) : Circle(radius) {
        this->name = name;
    }
    void show(){
        cout << "λ°˜μ§€λ¦„μ΄ " << getRadius() << "인 " << name << endl;
    }
};

int main()
{
    Named
Circle waffle(3, "waffle");
    waffle.show();
}

Β 

문제 : 2
NamedCircleν΄λž˜μŠ€μ™€ mainν•¨μˆ˜ λ“± ν•„μš”ν•œ ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄μ„œ 좜λ ₯ λ§Œλ“€μ–΄λ‚΄κΈ°

#include <iostream>
#include <string>
using namespace std;

class Circle{
    int radius;
public:
    Circle(int radius = 0) {this->radius = radius;}
    int getRadius() { return radius;}
    void setRadius(int radius) {this->radius = radius;}
    double getArea() {return 3.14 * radius * radius;}
};

class NamedCircle:public Circle{
    string name;
public:
    NamedCircle(){}
    NamedCircle(int radius, string name) : Circle(radius) {
        this->name = name;
    }
    void show(){
        cout << "λ°˜μ§€λ¦„μ΄ " << getRadius() << "인 " << name << endl;
    }
    void setName(string name){
        this->name = name;
    }
    string getName(){
        return name;
    }
};

int main()
{
    NamedCircle pizza[5];
    double max = 0;
    int maxIndex = 0;
    cout << "5 개의 μ •μˆ˜ λ°˜μ§€λ¦„κ³Ό μ›μ˜ 이름을 μž…λ ₯ν•˜μ„Έμš”" << endl;
    for(int i = 0; i < 5; i++)
    {
        string name;
        int radius;
        cout << i + 1 << ">> ";
        cin >> radius >> name;
        pizza[i].setName(name);
        pizza[i].setRadius(radius);
        if(pizza[i].getArea() > max)
        {
            max = pizza[i].getArea();
            maxIndex = i;
        }
    }
    cout << "κ°€μž₯ 면적이 큰 ν”ΌμžλŠ” " << pizza[maxIndex].getName() << "μž…λ‹ˆλ‹€" << endl;
}

Β 
μ‹€μŠ΅ 문제 8 - 3, 4 (Point클래슀)
문제 : 1
Point클래슀 μƒμ†λ°›λŠ” ColorPoint 클래슀 μ™„μ„±ν•˜κΈ°

#include <iostream>
#include <string>
using namespace std;

class Point{
    int x, y;
public:
    Point(int x, int y) {this->x = x; this->y = y;}
    int getX(){return x;}
    int getY(){return y;}
protected:
    void move(int x, int y) {this->x = x; this->y = y;}
};

class ColorPoint : public Point{
    string color;
public:
    ColorPoint(int x, int y, string color) : Point(x, y){
        this->color = color;
    }
    void setPoint(int x, int y) {move(x, y);}
    void setColor(string color) {this->color = color;}
    void show(){
        cout << color << "μƒ‰μœΌλ‘œ (" << getX() << ", " << getY() << ")에 μœ„μΉ˜ν•œ μ μž…λ‹ˆλ‹€." << endl;
    }
};

int main()
{
    ColorPoint cp(5, 5, "RED");
    cp.setPoint(10, 20);
    cp.setColor("BLUE");
    cp.show();
}

문제 : 4 mainν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λ„λ‘ ColorPoint클래슀 μ™„μ„±

#include <iostream>
#include <string>
using namespace std;

class Point{
    int x, y;
public:
    Point(int x, int y) {this->x = x; this->y = y;}
    int getX(){return x;}
    int getY(){return y;}
protected:
    void move(int x, int y) {this->x = x; this->y = y;}
};

class ColorPoint : public Point{
    string color;
public:
    ColorPoint(int x = 0, int y = 0, string color = "BLACK") : Point(x, y){
        this->color = color;
    }
    void setPoint(int x, int y) {move(x, y);}
    void setColor(string color) {this->color = color;}
    void show(){
        cout << color << "μƒ‰μœΌλ‘œ (" << getX() << ", " << getY() << ")에 μœ„μΉ˜ν•œ μ μž…λ‹ˆλ‹€." << endl;
    }
};

int main()
{
    ColorPoint zeroPoint;
    zeroPoint.show();
    
    ColorPoint cp(5, 5, "RED");
    cp.setPoint(10, 20);
    cp.setColor("BLUE");
    cp.show();
}

Β 

μ‹€μŠ΅ 문제 8 - 5, 6 (BaseArray 클래슀)
문제 : 1
BaseArrayλ₯Ό 상속받아 MyQueue클래슀 μ™„μ„±ν•˜κΈ°

#include <iostream>
using namespace std;

class BaseArray{
private:
    int capacity;
    int *mem;
protected:
    BaseArray(int capacity = 100){
        this->capacity = capacity;
        mem = new int[capacity];
    }
    ~BaseArray(){delete [] mem;}
    void put(int index, int val) {mem[index] = val;}
    int get(int index) {return mem[index];}
    int getCapacity(){return capacity;}
};

class MyQueue : protected BaseArray{
    int front;
    int rear;
public :
    MyQueue(int capacity) : BaseArray(capacity){ front = 0; rear = 0;}
    void enqueue(int val) {
        if(rear >= getCapacity()){
            cout << "더 이상 넣을 수 μ—†μŠ΅λ‹ˆλ‹€." << endl;
            return;
        }
        if(front > rear)
            front = 0;
        put(rear++, val);
    }
    int capacity() { return getCapacity(); }
    int length() { return rear; }
    int dequeue(){
        rear--;
        return get(front++);
    }
};

int main()
{
    MyQueue mQ(100);
    int n;
    cout << "큐에 μ‚½μž…ν•  5개의 μ •μˆ˜λ₯Ό μž…λ ₯ν•˜λΌ>> ";
    for (int i = 0; i < 5; i++) {
        cin >> n;
        mQ.enqueue(n);
    }
    cout << "큐의 μš©λŸ‰:" << mQ.capacity() << ", 큐의 크기:" << mQ.length() << endl;
    cout << "큐의 μ›μ†Œλ₯Ό μˆœμ„œλŒ€λ‘œ μ œκ±°ν•˜μ—¬ 좜λ ₯ν•œλ‹€>> ";
    while (mQ.length() != 0) {
        cout << mQ.dequeue() << ' ';
    }
    cout << endl << "큐의 ν˜„μž¬ 크기 : " << mQ.length() << endl;
}

Β 

문제 : 6
BaseArrayλ₯Ό 상속받아 MyStack클래슀 μ™„μ„±ν•˜κΈ°

#include <iostream>
using namespace std;

class BaseArray{
private:
    int capacity;
    int *mem;
protected:
    BaseArray(int capacity = 100){
        this->capacity = capacity;
        mem = new int[capacity];
    }
    ~BaseArray(){delete [] mem;}
    void put(int index, int val) {mem[index] = val;}
    int get(int index) {return mem[index];}
    int getCapacity(){return capacity;}
};

class MyStack : protected BaseArray{
    int tos;
public :
    MyStack(int capacity) : BaseArray(capacity){ tos = 0; }
    void enqueue(int val) { put(tos++, val); }
    int capacity() { return getCapacity(); }
    int length() { return tos; }
    int dequeue(){ return get(--tos);}
};

int main()
{
    MyStack mQ(100);
    int n;
    cout << "μŠ€νƒμ— μ‚½μž…ν•  5개의 μ •μˆ˜λ₯Ό μž…λ ₯ν•˜λΌ>> ";
    for(int i = 0; i < 5; i++){
        cin >> n;
        mQ.enqueue(n);
    }
    cout << "μŠ€νƒμ˜ μš©λŸ‰:" << mQ.capacity() << ", μŠ€νƒμ˜ 크기:" << mQ.length() << endl;
    cout << "μŠ€νƒμ˜ μ›μ†Œλ₯Ό μˆœμ„œλŒ€λ‘œ μ œκ±°ν•˜μ—¬ 좜λ ₯ν•œλ‹€>> ";
    while(mQ.length() != 0)
    {
        cout << mQ.dequeue() << ' ';
    }
    cout << endl << "μŠ€νƒμ˜ ν˜„μž¬ 크기 : " << mQ.length() << endl;
    
}

Β 
μ‹€μŠ΅ 문제 8 - 7
문제 : BaseMemory클래슀λ₯Ό μƒμ†λ°›λŠ” ROM, Ram클래슀 μ™„μ„±ν•˜κΈ°

#include <iostream>
using namespace std;

class BaseMemory{
    char *mem;
protected:
    BaseMemory(int size) { mem = new char[size];}
    void setInit(char a[], int index){
        for(int i = 0; i < index; i++)
        {
            mem[i] = a[i];
        }
    }
    void setChar(int i, char c) { mem[i] = c; }
    char getChar(int i) { return mem[i]; }
};
class ROM : protected BaseMemory{
public:
    ROM(int size, char a[], int index) : BaseMemory(size){
        setInit(a, index);
    }
    char read(int i){ return getChar(i); }
};
class RAM : protected BaseMemory{
public:
    RAM(int size) : BaseMemory(size){}
    void write(int i, char c) { setChar(i, c); }
    char read(int i) { return getChar(i); }
};

int main()
{
    char x[5] = {'H', 'e', 'l', 'l', 'o'};
    ROM biosROM(1024*10, x, 5);
    RAM mainMemory(1024 * 1024);
    for(int i = 0; i < 5; i++) mainMemory.write(i, biosROM.read(i));
    for(int i = 0; i < 5; i++) cout << mainMemory.read(i);
}

Β 
μ‹€μŠ΅ 문제 8 - 8
문제 : ν΄λž˜μŠ€μ™€ 상속을 μ΄μš©ν•΄ ν”„λ¦°ν„° μ™„μ„±ν•˜κΈ°

#include <iostream>
#include <string>
using namespace std;

class Printer{
    string model;
    string manufacturer;
    int printedCount = 0;
    int availableCount;
public:
    Printer(string model, string manufacturer, int availableCount){
        this->model = model;
        this->manufacturer = manufacturer;
        this->availableCount = availableCount;
    }
    bool print(int pages){
        if(availableCount - pages > 0)
        {
            return true;
        }
        return false;
    }
    void setPrintedCount(int pages){
        printedCount = pages;
    }
    int getAvailableCount(){
        availableCount -= printedCount;
        printedCount = 0;
        return availableCount;
    }
    string getModel(){
        return model;
    }
    string getManufacturer(){
        return manufacturer;
    }
};

class InkJetPrinter : public Printer{
    int availableInk;
public:
    InkJetPrinter(string model, string manufacturer, int availableCount, int availableInk)
    : Printer(model, manufacturer, availableCount) {this->availableInk = availableInk;}
    bool printInkJet(int pages){
        if(availableInk - pages > 0)
            return true;
        return false;
    }
    bool printing(int pages)
    {
        if(!print(pages)){
            cout << "μš©μ§€κ°€ λΆ€μ‘±ν•˜μ—¬ ν”„λ¦°νŠΈν•  수 μ—†μŠ΅λ‹ˆλ‹€." << endl;
            return false;
        }
        if(!printInkJet(pages)){
            cout << "μž‰ν¬κ°€ λΆ€μ‘±ν•˜μ—¬ ν”„λ¦°νŠΈν•  수 μ—†μŠ΅λ‹ˆλ‹€." << endl;
            return false;
        }
        setPrintedCount(pages);
        availableInk -= pages;
        return true;
    }
    void show(){
        cout << getModel() << ", " << getManufacturer() << ", 남은 쒅이 "
        << getAvailableCount() << "μž₯, 남은 μž‰ν¬ " << availableInk << endl;
    }
};

class LaserPrinter : public Printer{
    int availableToner;
public:
    LaserPrinter(string model, string manufacturer, int availableCount, int availableToner)
    : Printer(model, manufacturer, availableCount) {this->availableToner = availableToner;}
    bool printLaserPrint(int pages){
        if(availableToner - 1 > 0)
            return true;
        return false;
    }
    bool printing(int pages)
    {
        if(!print(pages)){
            cout << "μš©μ§€κ°€ λΆ€μ‘±ν•˜μ—¬ ν”„λ¦°νŠΈν•  수 μ—†μŠ΅λ‹ˆλ‹€." << endl;
            return false;
        }
        if(!printLaserPrint(pages)){
            cout << "μž‰ν¬κ°€ λΆ€μ‘±ν•˜μ—¬ ν”„λ¦°νŠΈν•  수 μ—†μŠ΅λ‹ˆλ‹€." << endl;
            return false;
        }
        setPrintedCount(pages);
        availableToner -= 1;
        return true;
    }
    void show(){
        cout << getModel() << ", " << getManufacturer() << ", 남은 쒅이 "
        << getAvailableCount() << "μž₯, 남은 μž‰ν¬ " << availableToner << endl;
    }
};

int main()
{
    char chk;
    InkJetPrinter *iJ;

    iJ = new InkJetPrinter("Officejet V40", "HP", 5, 10);
    LaserPrinter *lP;
    lP = new LaserPrinter("SCX-6x45", "μ‚Όμ„±μ „μž", 3, 20);
    cout << "ν˜„μž¬ μž‘λ™μ€‘μΈ 2 λŒ€μ˜ ν”„λ¦°ν„°λŠ” μ•„λž˜μ™€ κ°™λ‹€" << endl;
    cout << "μž‰ν¬μ ― : "; iJ->show();
    cout << "λ ˆμ΄μ € : "; lP->show();
    int print;
    int pages;
    while(true)
    {
        cout << "ν”„λ¦°ν„°(1: μž‰ν¬μ ―, 2:λ ˆμ΄μ €)와 맀수 μž…λ ₯>>";
        cin >> print >> pages;
        if(print == 1)
        {
            if(iJ->printing(pages)){
                cout << "ν”„λ¦°νŠΈν•˜μ˜€μŠ΅λ‹ˆλ‹€." << endl;
            }

        }
        else if(print == 2)
        {
            if(lP->printing(pages)){
                cout << "ν”„λ¦°νŠΈν•˜μ˜€μŠ΅λ‹ˆλ‹€." << endl;
            }
        }
        iJ->show();
        lP->show();

        cout << "계속 ν”„λ¦°νŠΈ ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ(y/n)>>";
        cin >> chk;
        if(chk == 'n'){
            break;
        }
        cout << endl;
    }

    delete lP;
    delete iJ;
}

Β 

μ‹€μŠ΅ 문제 8 - 9
문제 : λΉ„ν–‰κΈ° μ˜ˆμ•½ν”„λ‘œκ·Έλž¨ μž‘μ„±

Airplane.h

#ifndef UNTITLED3_AIRPLANE_H
#define UNTITLED3_AIRPLANE_H
using namespace std;

class Console{
    static int num;
    static string name;
    static int scheduleNum;
    static int seatNum;
public:
    static int setNum();
    static int setScheduleNum();
    static int setSeatNum();
    static string setName();
    static void Run();
    static void bookOrCancel();
};

class Seat{
    string seatName;
public:
    Seat();
    string getSeatName();
    void setSeatName(string seatName);
};
class Schedule : public Seat{
    Seat *seats;
public:
    Schedule();
    ~Schedule();
    void showSeat();
    Seat& getSeat(int index);
    void setSeat(int index, string seatName);
};
class AirlineBook : public Schedule, public Console{
    Schedule *schedules;
    int pickNum;
    int scheduleNum;
    string inputName;
public:
    AirlineBook();
    ~AirlineBook();
    void pick();
    void showSchedule();
    Schedule& getSchedule(int index);
};

#endif //UNTITLED3_AIRPLANE_H

Β 
Airplane.cpp

#include <iostream>
#include <string>
#include "Airplane.h"
using namespace std;
int Console::num = 0;
string Console::name;
int Console::scheduleNum = 0;
int Console::seatNum = 0;

int Console::setNum() {
    cin >> num;
    return num;
}
int Console::setScheduleNum() {
    cin >> scheduleNum;
    return scheduleNum;
}
int Console::setSeatNum() {
    cin >> seatNum;
    return seatNum;
}
string Console::setName() {
    cin >> name;
    return name;
}

void Console::Run() {
    cout << endl << "μ˜ˆμ•½:1, μ·¨μ†Œ:2, 보기:3, 끝내기:4>>";
}

void Console::bookOrCancel() {
    cout << "07μ‹œ:1, 12μ‹œ:2, 17μ‹œ:3>>";
}

Seat::Seat(){
    seatName = "---";
}

string Seat::getSeatName() {
    return seatName;
}

void Seat::setSeatName(string seatName) {
    this->seatName = seatName;
}

Schedule::Schedule() {
    seats = new Seat[8];
}
Schedule::~Schedule() {
    delete [] seats;
}
void Schedule::showSeat() {
    for(int i = 0; i < 8; i++){
        cout << "\t" << seats[i].getSeatName();
    }
    cout << endl;
}
Seat& Schedule::getSeat(int index){
    return this->seats[index];
}
void Schedule::setSeat(int index, string seatName){
    this->setSeatName(seatName);
}

AirlineBook::AirlineBook() {
    cout << "***** ν•œμ„±ν•­κ³΅μ— μ˜€μ‹ κ²ƒμ„ ν™˜μ˜ν•©λ‹ˆλ‹€ *****" << endl;
    schedules = new Schedule[3];
}

AirlineBook::~AirlineBook() {
    delete [] schedules;
}
Schedule& AirlineBook::getSchedule(int index){
    return this->schedules[index];
}

void AirlineBook::pick() {
    while(true){
        Run();
        pickNum = setNum();
        if(pickNum == 1){
            while(true){
                bookOrCancel();
                scheduleNum = setScheduleNum();
                if(scheduleNum == 1){
                    cout << "07μ‹œ:";
                    this->schedules[scheduleNum - 1].showSeat();
                    cout << "μ’Œμ„ 번호>> ";
                    pickNum = setNum();
                    cout << "이름 μž…λ ₯>> ";
                    inputName = setName();
                    this->schedules[scheduleNum - 1].getSeat(pickNum - 1).setSeatName(inputName);
                    break;
                }
                else if(scheduleNum == 2){
                    cout << "12μ‹œ:";
                    this->schedules[scheduleNum - 1].showSeat();
                    cout << "μ’Œμ„ 번호>> ";
                    pickNum = setNum();
                    cout << "이름 μž…λ ₯>> ";
                    inputName = setName();
                    this->schedules[scheduleNum - 1].getSeat(pickNum - 1).setSeatName(inputName);
                    break;
                }
                else if(scheduleNum == 3){
                    cout << "17μ‹œ:";
                    this->schedules[scheduleNum - 1].showSeat();
                    cout << "μ’Œμ„ 번호>> ";
                    pickNum = setNum();
                    cout << "이름 μž…λ ₯>> ";
                    inputName = setName();
                    this->schedules[scheduleNum - 1].getSeat(pickNum - 1).setSeatName(inputName);
                    break;
                }
                else{
                    cout << "잘λͺ»λœ μž…λ ₯μž…λ‹ˆλ‹€. λ‹€μ‹œ μž…λ ₯ν•΄μ£Όμ„Έμš”" << endl;
                }
            }
        }
        else if(pickNum == 2){
            while(true){
                bookOrCancel();
                scheduleNum = setScheduleNum();
                if(scheduleNum == 1){
                    cout << "07μ‹œ:";
                    this->schedules[scheduleNum - 1].showSeat();
                    cout << "μ’Œμ„ 번호>> ";
                    pickNum = setNum();
                    cout << "이름 μž…λ ₯>> ";
                    inputName = setName();
                    if(this->schedules[scheduleNum - 1].getSeat(pickNum - 1).getSeatName() == inputName)
                        this->schedules[scheduleNum - 1].getSeat(pickNum - 1).setSeatName("---");
                    break;
                }
                else if(scheduleNum == 2){
                    cout << "12μ‹œ:";
                    this->schedules[scheduleNum - 1].showSeat();
                    cout << "μ’Œμ„ 번호>> ";
                    pickNum = setNum();
                    cout << "이름 μž…λ ₯>> ";
                    inputName = setName();
                    if(this->schedules[scheduleNum - 1].getSeat(pickNum - 1).getSeatName() == inputName)
                        this->schedules[scheduleNum - 1].getSeat(pickNum - 1).setSeatName("---");
                    break;
                }
                else if(scheduleNum == 3){
                    cout << "17μ‹œ:";
                    this->schedules[scheduleNum - 1].showSeat();
                    cout << "μ’Œμ„ 번호>> ";
                    pickNum = setNum();
                    cout << "이름 μž…λ ₯>> ";
                    inputName = setName();
                    if(this->schedules[scheduleNum - 1].getSeat(pickNum - 1).getSeatName() == inputName)
                        this->schedules[scheduleNum - 1].getSeat(pickNum - 1).setSeatName("---");
                    break;
                }
                else{
                    cout << "잘λͺ»λœ μž…λ ₯μž…λ‹ˆλ‹€. λ‹€μ‹œ μž…λ ₯ν•΄μ£Όμ„Έμš”" << endl;
                }
            }
        }
        else if(pickNum == 3){
            showSchedule();
        }
        else if(pickNum == 4){
            cout << "μ˜ˆμ•½ μ‹œμŠ€ν…œμ„ μ’…λ£Œν•©λ‹ˆλ‹€.";
            break;
        }
        else
            cout << "잘λͺ»λœ μž…λ ₯μž…λ‹ˆλ‹€. λ‹€μ‹œ μž…λ ₯ν•΄μ£Όμ„Έμš”" << endl;
    }
}

void AirlineBook::showSchedule() {
    cout << "07μ‹œ: ";
    this->schedules[0].showSeat();
    cout << "12μ‹œ: ";
    this->schedules[1].showSeat();
    cout << "17μ‹œ: ";
    this->schedules[2].showSeat();
}

Β 
main.cpp

#include <iostream>
#include <string>
#include "Airplane.h"
using namespace std;

int main()
{
    AirlineBook* a;
    a = new AirlineBook;
    a->pick();
    delete a;
}
profile
μΉ˜νƒ€κ°€ 되고 싢은 취쀀생

0개의 λŒ“κΈ€