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

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

9μž₯ μš”μ 

κ°€μƒν•¨μˆ˜μ™€ μΆ”μƒν΄λž˜μŠ€

κ°€μƒν•¨μˆ˜

상속 κ΄€κ³„μ—μ„œμ˜ ν•¨μˆ˜ μž¬μ •μ˜

μžμ‹ ν΄λž˜μŠ€μ—μ„œ λΆ€λͺ¨ν΄λž˜μŠ€μ— μžˆλŠ” ν•¨μˆ˜λ₯Ό λ™μΌν•œ μ›ν˜•μœΌλ‘œ μž¬μ •μ˜ ν•œλ‹€.
μžμ‹ 클래슀의 ν¬μΈν„°λ‘œ μžμ‹ ν΄λž˜μŠ€μ— μž‘μ„±λœ ν•¨μˆ˜λ₯Ό 뢈러였고 λΆ€λͺ¨ 클래슀의 멀버 ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜κ³  싢은 경우 μ—…μΊμŠ€νŒ…μ„ 톡해 λΆ€λͺ¨ ν΄λž˜μŠ€μ— λŒ€ν•œ ν¬μΈν„°λ‘œ λ§Œλ“  ν›„ ν˜ΈμΆœν•œλ‹€.

μ˜ˆμ‹œ

Derived d, *pDer;
pDer = &d;
pDer->f(); // μžμ‹ 클래슀의 f()λ₯Ό 호좜

Base* pBase;
pBase = pDer; // μ—…μΊμŠ€νŒ…
pBase->f(); // λΆ€λͺ¨ 클래슀의 f()λ₯Ό 호좜

가상 ν•¨μˆ˜μ™€ μ˜€λ²„λΌμ΄λ”©

κ°€μƒν•¨μˆ˜

virtual ν‚€μ›Œλ“œλ‘œ μ„ μ–Έλœ 멀버 ν•¨μˆ˜
μ„ μ–Έ

virtual void f();

μ˜€λ²„λΌμ΄λ”©

μžμ‹ ν΄λž˜μŠ€μ—μ„œ λΆ€λͺ¨ 클래슀의 가상 ν•¨μˆ˜λ₯Ό μž¬μ •μ˜ν•˜λŠ” 것

⚠️ ν•¨μˆ˜ μž¬μ •μ˜μ™€ μ˜€λ²„λΌμ΄λ”©μ˜ 차이

ν•¨μˆ˜ μž¬μ •μ˜λŠ” μ›λž˜ μ •μ˜ν–ˆλ˜ ν•¨μˆ˜μ— 접근이 κ°€λŠ₯ν•˜μ§€λ§Œ.
μ˜€λ²„λΌμ΄λ”©μ„ ν•˜λ©΄ μ›λž˜ μ •μ˜ν–ˆλ˜ ν•¨μˆ˜μ˜ 쑴재감이 μ‚¬λΌμ§€κ²Œ λœλ‹€.
ν•¨μˆ˜ μž¬μ •μ˜λŠ” μ •μ λ°”μΈλ”©μœΌλ‘œ 컴파일 μ‹œκ°„μ— κ²°μ •
μ˜€λ²„λΌμ΄λ”©μ€ λ™μ λ°”μΈλ”©μœΌλ‘œ μ‹€ν–‰ μ‹œκ°„μ— κ²°μ •

μ˜€λ²„λΌμ΄λ”© λͺ©μ 

λΆ€λͺ¨ ν΄λž˜μŠ€μ—μ„œλŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ œκ³΅ν•˜κ³  μžμ‹ ν΄λž˜μŠ€μ—μ„œ μ˜€λ²„λΌμ΄λ”©ν•˜μ—¬ λ‹€ν˜•μ„±μ„ μ‹€ν˜„ν•˜λŠ” 것

동적 바인딩

바인딩 : λ³€μˆ˜μ˜ νƒ€μž…μ„ κ²°μ • ν•˜λ“― ν”„λ‘œκ·Έλž¨ ꡬ성 μš”μ†Œμ˜ 성격을 κ²°μ •ν•΄μ£ΌλŠ” 것
동적 바인딩 : μ»΄νŒŒμΌλŸ¬κ°€ κ°€μƒν•¨μˆ˜λ₯Ό 보고 μš°μ„  λ„˜μ–΄κ°„ λ‹€μŒ λ‚˜μ€‘μ— μ‹€ν–‰μ‹œκ°„μ— κ°€μƒν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜κ²Œ 되면, 객체 내에 μ˜€λ²„λΌμ΄λ”©λœ 가상 ν•¨μˆ˜λ₯Ό λ™μ μœΌλ‘œ μ°Ύμ•„
ν˜ΈμΆœν•˜λŠ” 것

동적 바인딩이 λ°œμƒν•œλŠ” 경우

  1. λΆ€λͺ¨ 클래슀 λ‚΄μ˜ 멀버 ν•¨μˆ˜κ°€ 가상 ν•¨μˆ˜ 호좜
  2. μžμ‹ 클래슀 λ‚΄μ˜ 멀버 ν•¨μˆ˜κ°€ 가상 ν•¨μˆ˜ 호좜
  3. μ™ΈλΆ€ ν•¨μˆ˜μ—μ„œ λΆ€λͺ¨ 클래슀의 ν¬μΈν„°λ‘œ 가상 ν•¨μˆ˜ 호좜
  4. λ‹€λ₯Έ ν΄λž˜μŠ€μ—μ„œ 가상 ν•¨μˆ˜ 호좜

상속 κ΄€λ ¨ λͺ…λ Ήμ–΄ override, final

// λΆ€λͺ¨ 클래슀 가상 멀버 ν•¨μˆ˜
virtual void draw();

// μžμ‹ 클래슀
void drow(); 
// drawλ₯Ό drow둜 잘λͺ»μ³€λ‹€. 이 경우 drowλΌλŠ” 멀버 ν•¨μˆ˜λ‘œ ν˜ΈμΆœλ˜μ–΄ μ»΄νŒŒμΌλŸ¬κ°€ 였λ₯˜ λ°œμƒν•˜μ§€ μ•ŠλŠ”λ‹€.

// override
// void drow() override;
// μ»΄νŒŒμΌλŸ¬κ°€ ν•¨μˆ˜κ°€ μ˜€λ²„λΌμ΄λ”©μ΄ λ˜μ§€ μ•Šμ•˜μœΌλ―€λ‘œ 였λ₯˜λ₯Ό μž‘μ•„μ€€λ‹€.
//final
//상속 및 μž¬μ •μ˜ λ§‰λŠ” μ§€μ‹œμ–΄, 클래슀 이름, 가상 ν•¨μˆ˜ μ›ν˜•μ˜ 뒀에 μž‘μ„±

class Student final{ // Student클래슀 상속 λΆˆκ°€
	virtual void show(); // showν•¨μˆ˜ μ˜€λ²„λΌμ΄λ”© λΆˆκ°€
};

μ˜€λ²„λΌμ΄λ”©μ˜ νŠΉμ§•

  1. μ˜€λ²„λΌμ΄λ”© μ‹œ 이름, λ§€κ°œλ³€μˆ˜ 개수 νƒ€μž…, λ¦¬ν„΄νƒ€μž…μ΄ μΌμΉ˜ν•΄μ•Όν•œλ‹€.
  2. λΆ€λͺ¨ν΄λž˜μŠ€μ—μ„œλŠ” virtual이 μžˆμ–΄μ•Ό ν•˜μ§€λ§Œ, μžμ‹ν΄λž˜μŠ€μ—μ„œλŠ” virtual μƒλž΅ν•΄λ„ λœλ‹€.
  3. μ ‘κ·Ό μ§€μ •μž μ‚¬μš© κ°€λŠ₯

β—οΈμ˜€λ²„λΌμ΄λ”©μ— μ˜ν•΄ 쑴재감이 없어진 λΆ€λͺ¨ 클래슀의 κ°€μƒν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜κ³  μ‹Άλ‹€λ©΄,
(::)λ₯Ό μ΄μš©ν•˜μ—¬ μ •μ λ°”μΈλ”©μœΌλ‘œ ν˜ΈμΆœν•˜λ©΄ λœλ‹€.

κ°€μƒμ†Œλ©Έμž

정상적인 μ†Œλ©Έμ„ μœ„ν•΄μ„œλŠ” λΆ€λͺ¨ 클래슀의 μ†Œλ©Έμžλ₯Ό 가상 μ†Œλ©Έμžλ‘œ κ΅¬ν˜„ν•˜λŠ” 것이 μ’‹λ‹€.
μ—…μΊμŠ€νŒ…μ„ ν•œ ν›„ μ†Œλ©Έμžλ₯Ό 뢈러였게 되면 λΆ€λͺ¨ν΄λž˜μŠ€μ˜ μ†Œλ©Έμžλ§Œ λΆˆλŸ¬μ™€μ§„λ‹€.
λ•Œλ¬Έμ—, κ°€μƒμ†Œλ©Έμžλ‘œ κ΅¬ν˜„ν•˜μ—¬ μžμ‹ 클래슀의 μ†Œλ©Έμžλ₯Ό λ™μ λ°”μΈλ”©μœΌλ‘œ λΆˆλŸ¬μ˜¨λ‹€.
μ–΄μ§œν”Ό, μžμ‹ ν΄λž˜μŠ€κ°€ μ†Œλ©Έλ˜λ©΄ λΆ€λͺ¨ 클래슀의 μ†Œλ©ΈμžλŠ” μžλ™μœΌλ‘œ μ‹€ν–‰λœλ‹€.

μΆ”μƒν΄λž˜μŠ€

순수 가상 ν•¨μˆ˜

μ–΄μ§œν”Ό μΈν„°νŽ˜μ΄μŠ€μ˜ μ—­ν• μ΄λ―€λ‘œ,
ν•¨μˆ˜μ˜ μ½”λ“œκ°€ μ—†κ³  μ„ μ–Έλ§Œ μžˆλŠ” κ°€μƒν•¨μˆ˜λ₯Ό λ§ν•œλ‹€.
λ”°λΌμ„œ λΆ€λͺ¨ ν΄λž˜μŠ€μ—μ„œ κ΅¬ν˜„μ„ ν•΄μ„œλŠ” μ•ˆλœλ‹€.

virtual void foo() = 0; // μ„ μ–Έ

μΆ”μƒν΄λž˜μŠ€

μ΅œμ†Œ ν•˜λ‚˜μ˜ 순수 가상 ν•¨μˆ˜λ₯Ό 가진 클래슀λ₯Ό 좔상 클래슀라고 ν•œλ‹€.

class Student{
	virtual void foo() = 0;
};

Student ν΄λž˜μŠ€λŠ” fooλΌλŠ” 순수 가상 ν•¨μˆ˜λ₯Ό κ°€μ§€λ―€λ‘œ μΆ”μƒν΄λž˜μŠ€λ‹€.
⚠️ μΆ”μƒν΄λž˜μŠ€λŠ” λΆˆμ™„μ „ν•œ ν΄λž˜μŠ€μ΄λ―€λ‘œ 객체λ₯Ό 생성할 수 μ—†λ‹€.
단, 포인터 선언은 λ¬Έμ œλ˜μ§€ μ•ŠλŠ”λ‹€.

μΆ”μƒν΄λž˜μŠ€ λͺ©μ 

상속을 μœ„ν•œ κΈ°λ³Έ 클래슀둜 ν™œμš©ν•˜λŠ” 것이 λͺ©μ .
즉, μžμ‹ ν΄λž˜μŠ€λ“€μ΄ κ΅¬ν˜„μ„ ν•΄μ•Όν•  ν•¨μˆ˜μ˜ μ›ν˜•μ„ 보여주기 μœ„ν•œ μΈν„°νŽ˜μ΄μŠ€μ˜ 역할이 λͺ©μ μ΄λ‹€.
κ·Έλ ‡λ‹€κ³  ν•΄μ„œ μΆ”μƒν΄λž˜μŠ€κ°€ 멀버 λ³€μˆ˜, ν•¨μˆ˜λ₯Ό 가지지 μ•ŠλŠ” 것은 μ•„λ‹ˆλ‹€. νŒŒμƒν΄λž˜μŠ€κ°€ κ΅¬ν˜„μ„ ν•΄μ•Όν•˜λŠ” 순수 가상 ν•¨μˆ˜λ₯Ό 가진것 뿐이닀.

μΆ”μƒν΄λž˜μŠ€ 상속

좔상 클래슀λ₯Ό 상속 λ°›μœΌλ©΄ 상속받은 μžμ‹ ν΄λž˜μŠ€λ„ 좔상 ν΄λž˜μŠ€κ°€ λœλ‹€.
λ”°λΌμ„œ μžμ‹ ν΄λž˜μŠ€λ„ 객체 생성이 λΆˆκ°€λŠ₯ ν•˜λ‹€.

μΆ”μƒν΄λž˜μŠ€μ˜ κ΅¬ν˜„

상속받은 μžμ‹ 클래슀λ₯Ό μ‚¬μš©ν•˜κ³  μ‹Άλ‹€λ©΄, λΆ€λͺ¨ 클래슀의 λͺ¨λ“  순수 κ°€μƒν•¨μˆ˜λ₯Ό μ˜€λ²„λΌμ΄λ”©μ„ ν•΄μ•Ό μ‚¬μš©ν•  수 μžˆλ‹€.

μ˜ˆμ‹œ

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

class Subject{ // 좔상 클래슀
public:
	virtual void show() = 0;
};

class Korean : public Subject{ // μžμ‹ 클래슀
public:
	virtual void show() {cout << "κ΅­μ–΄" << endl;}
    // 순수 κ°€μƒν•¨μˆ˜ κ΅¬ν˜„
};

int main(){
	Korean korea; // μ‚¬μš© κ°€λŠ₯
    Korean *k = new Korean; // μ‚¬μš© κ°€λŠ₯
    k->show();
    korea.show();
}

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

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

μ‹€μŠ΅ 문제 9 - 1, 2 (Converter μΆ”μƒν΄λž˜μŠ€)
문제 : 1
Converter클래슀λ₯Ό 상속받아 λ‹¬λŸ¬λ₯Ό μ›ν™”λ‘œ ν™˜μ‚°ν•˜λŠ” WonToDollar클래슀λ₯Ό μž‘μ„±ν•˜κΈ°.

#include <iostream>
using namespace std;

class Converter{
protected:
    double ratio;
    virtual double convert(double src)=0;
    virtual string getSourceString()=0;
    virtual string getDestString()=0;
public:
    Converter(double ratio){ this->ratio = ratio;}
    void run(){
        double src;
        cout << getSourceString() << "을 " << getDestString() << "둜 λ°”κΏ‰λ‹ˆλ‹€. ";
        cout << getSourceString() << "을 μž…λ ₯ν•˜μ„Έμš”>> ";
        cin >> src;
        cout << "λ³€ν™˜ κ²°κ³Ό : " << convert(src) << getDestString() << endl;
    }
};

class WonToDollar : public Converter{
public:
    WonToDollar(double ratio) : Converter(ratio) {}
    double convert(double src) override{
        return src / ratio;
    }
    string getSourceString() override{
        return "원";
    }
    string getDestString() override{
        return "λ‹¬λŸ¬";
    }
};
int main()
{
    WonToDollar wd(1010);
    wd.run();
}

Β 

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

#include <iostream>
using namespace std;

class Converter{
protected:
    double ratio;
    virtual double convert(double src)=0;
    virtual string getSourceString()=0;
    virtual string getDestString()=0;
public:
    Converter(double ratio){ this->ratio = ratio;}
    void run(){
        double src;
        cout << getSourceString() << "을 " << getDestString() << "둜 λ°”κΏ‰λ‹ˆλ‹€. ";
        cout << getSourceString() << "을 μž…λ ₯ν•˜μ„Έμš”>> ";
        cin >> src;
        cout << "λ³€ν™˜ κ²°κ³Ό : " << convert(src) << getDestString() << endl;
    }
};

class WonToDollar : public Converter{
public:
    WonToDollar(double ratio) : Converter(ratio) {}
    double convert(double src) override{
        return src / ratio;
    }
    string getSourceString() override{
        return "원";
    }
    string getDestString() override{
        return "λ‹¬λŸ¬";
    }
};

class KmToMile : public Converter{
public:
    KmToMile(double ratio) : Converter(ratio){}
    double convert(double src) override{
        return src / ratio;
    }
    string getSourceString() override{
        return "Km";
    }
    string getDestString() override{
        return "Mile";
    }
};
int main()
{
    KmToMile toMile(1.609344);
    toMile.run();
}

Β 
μ‹€μŠ΅ 문제 9 - 3, 4 (LoopAdder 클래슀)
문제 : 3
LoopAdder클래슀λ₯Ό μƒμ†λ°›λŠ” ForLoopAdder클래슀 μž‘μ„±ν•˜κΈ°

#include <iostream>
using namespace std;

class LoopAdder{
    string name;
    int x, y, sum;
    void read();
    void write();
protected:
    LoopAdder(string name=""){
        this->name = name;
    }
    int getX(){ return x; }
    int getY(){ return y; }
    virtual int calculate() = 0;
public:
    void run();
};

void LoopAdder::read() {
    cout << name << ":" << endl;
    cout << "처음 μˆ˜μ—μ„œ λ‘λ²ˆμ§Έ μˆ˜κΉŒμ§€ λ”ν•©λ‹ˆλ‹€. 두 수λ₯Ό μž…λ ₯ν•˜μ„Έμš” >> ";
    cin >> x >> y;
}

void LoopAdder::write() {
    cout << x << "μ—μ„œ " << y << "κΉŒμ§€μ˜ ν•© = " << sum << " μž…λ‹ˆλ‹€" << endl;
}

void LoopAdder::run() {
    read();
    sum = calculate();
    write();
}

class ForLoopAdder : public LoopAdder{
public:
    ForLoopAdder(string name) : LoopAdder(name){}
    int calculate() override{
        int result = 0;
        for(int i = getX(); i <= getY(); i++)
        {
            result += i;
        }
        return result;
    }
};

int main()
{
    ForLoopAdder forLoop("For Loop");
    forLoop.run();
}

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

#include <iostream>
using namespace std;

class LoopAdder{
    string name;
    int x, y, sum;
    void read();
    void write();
protected:
    LoopAdder(string name=""){
        this->name = name;
    }
    int getX(){ return x; }
    int getY(){ return y; }
    virtual int calculate() = 0;
public:
    void run();
};

void LoopAdder::read() {
    cout << name << ":" << endl;
    cout << "처음 μˆ˜μ—μ„œ λ‘λ²ˆμ§Έ μˆ˜κΉŒμ§€ λ”ν•©λ‹ˆλ‹€. 두 수λ₯Ό μž…λ ₯ν•˜μ„Έμš” >> ";
    cin >> x >> y;
}

void LoopAdder::write() {
    cout << x << "μ—μ„œ " << y << "κΉŒμ§€μ˜ ν•© = " << sum << " μž…λ‹ˆλ‹€" << endl;
}

void LoopAdder::run() {
    read();
    sum = calculate();
    write();
}

class WhileLoopAdder : public LoopAdder{
public :
    WhileLoopAdder(string name) : LoopAdder(name){}
    int calculate() override{
        int i = getX(), result = 0;
        while(i <= getY()){
            result += i;
            i++;
        }
        return result;
    }
};

class DoWhileLoopAdder : public LoopAdder{
public:
    DoWhileLoopAdder(string name) : LoopAdder(name){}
    int calculate() override{
        int i = getX(), result = 0;
        do
        {
            result += i;
            i++;
        }while(i <= getY());
        return result;
    }
};
int main()
{
    WhileLoopAdder whileLoop("While Loop");
    DoWhileLoopAdder doWhileLoop("Do while Loop");

    whileLoop.run();
    doWhileLoop.run();
}

Β 

μ‹€μŠ΅ 문제 9 - 5
문제 : AbstractGate을 상속받은 ORGate, XORGate, ANDGate μž‘μ„±ν•˜κΈ°

#include <iostream>
using namespace std;

class AbstractGate{
protected:
    bool x, y;
public:
    void set(bool x, bool y){
        this->x = x;
        this->y = y;
    }
    virtual bool operation() = 0;
};
class ANDGate : public AbstractGate{
public:
    bool operation() override{
        return x && y;
    }
};

class ORGate : public AbstractGate{
public:
    bool operation() override{
        return x || y;
    }
};

class XORGate : public AbstractGate{
public:
    bool operation() override{
        return x != y;
    }
};
int main()
{
    ANDGate andGate;
    ORGate orGate;
    XORGate xorGate;

    andGate.set(true, false);
    orGate.set(true, false);
    xorGate.set(true, false);

    cout.setf(ios::boolalpha);
    cout << andGate.operation() << endl;
    cout << orGate.operation() << endl;
    cout << xorGate.operation() << endl;
}

Β 
μ‹€μŠ΅ 문제 9 - 6
문제 : AbsractStack을 상속받은 IntStack클래슀 λ§Œλ“€κΈ°

#include <iostream>
using namespace std;

class AbstractStack{
public:
    virtual bool push(int n) = 0;
    virtual bool pop(int &n) = 0;

    virtual int size() = 0;
};

class IntStack : public AbstractStack{
    int tos;
    int *arr;
    int mSize;
public:
    IntStack(int size){
        mSize = size;
        arr = new int[size];
        tos = -1;
    }
    ~IntStack(){ delete [] arr; }
    bool push(int n) override{
        if(tos + 1 == mSize)
            return false;
        else{
            arr[++tos] = n;
            cout << "ν˜„μž¬ tos값은 " << tos << "이고 λ“€μ–΄μžˆλŠ” 값은 " << n << "μž…λ‹ˆλ‹€." << endl;
            return true;
        }
    }
    bool pop(int &n) override{
        if(tos < 0)
            return false;
        else
        {
            cout << "이전 tos값은" << tos << "μ˜€κ³ , ";
            n = arr[tos--];
            cout << "ν˜„μž¬ tos값은 " << tos << "이고 λ“€μ–΄μžˆλ˜ 값은";
            return true;
        }
    }
    int size() override{
        return tos + 1;
    }
};

int main()
{
    int n;
    cout << "μŠ€νƒμ˜ 개수λ₯Ό μž…λ ₯ν•˜μ„Έμš” : ";
    cin >> n;
    IntStack stack(n);
    cout << "μ§€κΈˆ μŠ€νƒμ˜ ν¬κΈ°λŠ” " << stack.size() << endl;
    int a;
    cout << "μŠ€νƒμ— μ±„μ›Œ 넣을 수λ₯Ό μž…λ ₯ν•˜μ„Έμš” : ";
    for(int i = 0; i < n; i++)
    {
        cin >> a;
        stack.push(a);
    }
    cout << "ν‘Έμ‹œ ν›„ μŠ€νƒμ˜ ν¬κΈ°λŠ” " << stack.size() << endl;
    cout << "팝" << endl;
    for(int i = 0; i < n; i++)
    {
        stack.pop(a);
        cout << a << "μž…λ‹ˆλ‹€." << endl;
    }
    cout << "νŒν•œ ν›„ μŠ€νƒμ˜ ν¬κΈ°λŠ” " << stack.size() << endl;

}

Β 
μ‹€μŠ΅ 문제 9 - 7, 8 (LoopAdder 클래슀)
문제 : 7
Shape클래슀λ₯Ό μƒμ†λ°›λŠ” Oval, Rect, Triangular클래슀 μ™„μ„±ν•˜κΈ°

#include <iostream>
using namespace std;

class Shape{
protected:
    string name;
    int width, height;
public:
    Shape(string n = "", int w = 0, int h = 0){ name = n; width = w, height = h;}
    virtual double getArea(){ return 0; }
    string getName(){ return name; }
};

class Oval : public Shape{
public:
    Oval(string n = "", int w = 0, int h = 0) : Shape(n, w, h) {}
    double getArea() override{
        return 3.14 * height * width; // 타원 넓이 곡식
    }
};

class Rect : public Shape{
public:
    Rect(string n = "", int w = 0, int h = 0) : Shape(n, w, h){}
    double getArea() override{
        return height * width;
    }
};

class Triangular : public Shape{
public:
    Triangular(string n = "", int w = 0, int h = 0) : Shape(n, w, h){}
    double getArea() override{
        return 0.5 * height * width;
    }
};

int main()
{
    Shape *p[3];
    p[0] = new Oval("λΉˆλŒ€λ–‘", 10, 20);
    p[1] = new Rect("μ°°λ–‘", 30, 40);
    p[2] = new Triangular("ν† μŠ€νŠΈ", 30, 40);

    for(int i = 0; i < 3; i++)
        cout << p[i]->getName() << " λ„“μ΄λŠ” " << p[i]->getArea() << endl;

    for(int i = 0; i < 3; i++)
        delete p[i];
}

Β 

문제 : 8
Shape클래슀λ₯Ό μΆ”μƒν΄λž˜μŠ€λ‘œ λ§Œλ“€κΈ°

#include <iostream>
using namespace std;

class Shape{
protected:
    string name;
    int width, height;
public:
    Shape(string n = "", int w = 0, int h = 0){ name = n; width = w, height = h;}
    virtual ~Shape(){}
    virtual double getArea() = 0;
    string getName(){ return name; }
};

class Oval : public Shape{
public:
    Oval(string n = "", int w = 0, int h = 0) : Shape(n, w, h) {}
    double getArea() override{
        return 3.14 * height * width; // 타원 넓이 곡식
    }
};

class Rect : public Shape{
public:
    Rect(string n = "", int w = 0, int h = 0) : Shape(n, w, h){}
    double getArea() override{
        return height * width;
    }
};

class Triangular : public Shape{
public:
    Triangular(string n = "", int w = 0, int h = 0) : Shape(n, w, h){}
    double getArea() override{
        return 0.5 * height * width;
    }
};

int main()
{
    Shape *p[3];
    p[0] = new Oval("λΉˆλŒ€λ–‘", 10, 20);
    p[1] = new Rect("μ°°λ–‘", 30, 40);
    p[2] = new Triangular("ν† μŠ€νŠΈ", 30, 40);

    for(int i = 0; i < 3; i++)
        cout << p[i]->getName() << " λ„“μ΄λŠ” " << p[i]->getArea() << endl;
    for(int i = 0; i < 3; i++){
        delete p[i];
    }
    return 0;
}

μ‹€μŠ΅ 문제 9 - 9
문제 : Printer클래슀λ₯Ό μƒμ†λ°›λŠ” InkJetPrinter, LaserPrinter클래슀 λ§Œλ“€κΈ°

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

class Printer{
protected:
    string model;
    string manufacturer;
    int printedCount;
    int availablecount;
public:
    Printer(string model, string manufacturer, int availablecount) {
        this->model = model; this->manufacturer = manufacturer; this->availablecount = availablecount;
    }
    virtual ~Printer(){}
    virtual void print(int pages) = 0;
    virtual void show() = 0;
};
class InkJetPrinter : public Printer{
    int availableInk;
public:
    InkJetPrinter(string model, string manufacturer, int availablecount, int availableInk)
    : Printer(model, manufacturer, availablecount){
        this->availableInk = availableInk;
    }
    void print(int pages) override{
        printedCount = pages;
        if(printedCount < availableInk && availablecount > printedCount)
        {
            cout << "ν”„λ¦°νŠΈν•˜μ˜€μŠ΅λ‹ˆλ‹€." << endl;
            availableInk -= printedCount;
            availablecount -= printedCount;
        }
        else
            cout << "μš©μ§€κ°€ λΆ€μ‘±ν•˜μ—¬ ν”„λ¦°νŠΈν•  수 μ—†μŠ΅λ‹ˆλ‹€." << endl;
    }
    void show() override{
        cout << "μž‰ν¬μ ― : " << model << ", " << manufacturer << ", " << "남은 쒅이 " << availablecount << "μž₯, "
        << "남은 μž‰ν¬ " << 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;
    }
    void print(int pages) override{
        printedCount = pages;
        if(printedCount < availableToner && availablecount > printedCount)
        {
            cout << "ν”„λ¦°νŠΈν•˜μ˜€μŠ΅λ‹ˆλ‹€." << endl;
            availableToner -= 1;
            availablecount -= printedCount;
        }
        else
            cout << "μš©μ§€κ°€ λΆ€μ‘±ν•˜μ—¬ ν”„λ¦°νŠΈν•  수 μ—†μŠ΅λ‹ˆλ‹€." << endl;
    }
    void show() override{
        cout << "μž‰ν¬μ ― : " << model << ", " << manufacturer << ", " << "남은 쒅이 " << availablecount << "μž₯, "
             << "남은 ν† λ„ˆ " << availableToner << endl;
    }
};

int main()
{
    Printer *p[2];
    p[0] = new InkJetPrinter("Officejet V40", "HP", 5, 10);
    p[1] = new LaserPrinter("SCX-6x45", "μ‚Όμ„±μ „μž", 3, 20);

    for(int i = 0; i < 2; i++)
    {
        p[i]->show();
    }

    int printer;
    int paper;
    char chk;

    while(true){
        cout << "ν”„λ¦°ν„°(1:μž‰ν¬μ ―, 2:λ ˆμ΄μ €)와 맀수 μž…λ ₯>>";
        cin >> printer >> paper;
        p[printer - 1]->print(paper);

        for(int i = 0; i < 2; i++)
        {
            p[i]->show();
        }
        cout << "계속 ν”„λŸ°νŠΈ ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ(y/n)>>";
        cin >> chk;
        if(chk == 'n')
            break;
        cout << endl;
    }
    for(int i = 0; i < 2; i++)
    {
        delete p[i];
    }
    return 0;
}

μ‹€μŠ΅ 문제 9 - 10
문제 : κ·Έλž˜ν”½ νŽΈμ§‘κΈ°λ₯Ό μ½˜μ†” λ°”νƒ•μœΌλ‘œ λ§Œλ“€κΈ°, μ—°κ²°λ¦¬μŠ€νŠΈ ν™œμš©

μ—…λ‘œλ“œμ€‘..

#include <iostream>
using namespace std;
class Node{
public:
    string name;
    Node *next;
};
class Graphic{
protected:
    static int index;
    static Node *first;
    static Node *last;
public:
    virtual void insert(Node *node) = 0;
    void del(int delNum){
        Node *p = first;
        Node *q;
        for(int i = 0; i < index; i++)
        {
            if(i == delNum)
            {
                if(i == 0)
                {
                    first->name = p->name;
                    first->next = p->next;
                }
                else
                {
                    q->next = p->next;
                }
                index--;
                delete p;
                break;
            }
            q = p;
            p = p->next;
        }
    }
    void show(){
        Node *temp = new Node;
        temp->name = first->name;
        temp->next = first->next;
        for(int i = 0; i < index; i++)
        {
            cout << i << ": " << temp->name << endl;
            temp = temp->next;
        }
    }
};
class Line : public Graphic{
public:
    void insert(Node *node) override{
        node->name = "Line";
        node->next = nullptr;
        if(first == nullptr)
            first = node;
        else
            last->next = node;
        last = node;
        index++;
    }
};
class Circle : public Graphic{
public:
    void insert(Node *node) override{
        node->name = "Circle";
        node->next = nullptr;
        if(first == nullptr)
            first = node;
        else
            last->next = node;
        last = node;
        index++;
    }
};
class Rectangle : public Graphic{
public:
    void insert(Node *node) override{
        node->name = "Rectangle";
        node->next = nullptr;
        if(first == nullptr)
            first = node;
        else
            last->next = node;
        last = node;
        index++;
    }
};
int Graphic::index = 0;
Node* Graphic::first = nullptr;
Node* Graphic::last = nullptr;
int main()
{
    cout << "κ·Έλž˜ν”½ μ—λ””ν„°μž…λ‹ˆλ‹€." << endl;
    int n;
    Graphic *graphic;
    while(true)
    {
        cout << "μ‚½μž…:1, μ‚­μ œ:2, λͺ¨λ‘λ³΄κΈ°:3, μ’…λ£Œ:4 >> ";
        cin >> n;
        if( n == 1){
            Node *node = new Node;
            int a;
            cout << "μ„ :1, 원:2, μ‚¬κ°ν˜•:3 >> ";
            cin >> a;
            if(a == 1)
            {
                graphic = new Line;
                graphic->insert(node);
            }
            if(a == 2){
                graphic = new Circle;
                graphic->insert(node);
            }
            if(a == 3){
                graphic = new Rectangle;
                graphic->insert(node);
            }
        }
        if(n == 2){
            int a;
            cout << "μ‚­μ œν•˜κ³ μž ν•˜λŠ” λ„ν˜•μ˜ 인덱슀 >> ";
            cin >> a;
            graphic->del(a);
        }
        if(n == 3){
            graphic->show();
        }
        if(n == 4){
            break;
        }
    }
}
profile
μΉ˜νƒ€κ°€ 되고 싢은 취쀀생

0개의 λŒ“κΈ€