9μ₯ μμ
μμ κ΄κ³μμμ ν¨μ μ¬μ μ
μμ ν΄λμ€μμ λΆλͺ¨ν΄λμ€μ μλ ν¨μλ₯Ό λμΌν μνμΌλ‘ μ¬μ μ νλ€.
μμ ν΄λμ€μ ν¬μΈν°λ‘ μμ ν΄λμ€μ μμ±λ ν¨μλ₯Ό λΆλ¬μ€κ³ λΆλͺ¨ ν΄λμ€μ λ©€λ² ν¨μλ₯Ό νΈμΆνκ³ μΆμ κ²½μ° μ μΊμ€ν μ ν΅ν΄ λΆλͺ¨ ν΄λμ€μ λν ν¬μΈν°λ‘ λ§λ ν νΈμΆνλ€.
μμ
Derived d, *pDer;
pDer = &d;
pDer->f(); // μμ ν΄λμ€μ f()λ₯Ό νΈμΆ
Base* pBase;
pBase = pDer; // μ
μΊμ€ν
pBase->f(); // λΆλͺ¨ ν΄λμ€μ f()λ₯Ό νΈμΆ
κ°μ ν¨μμ μ€λ²λΌμ΄λ©
κ°μν¨μ
virtual ν€μλλ‘ μ μΈλ λ©€λ² ν¨μ
μ μΈvirtual void f();
μ€λ²λΌμ΄λ©
μμ ν΄λμ€μμ λΆλͺ¨ ν΄λμ€μ κ°μ ν¨μλ₯Ό μ¬μ μνλ κ²
β οΈ ν¨μ μ¬μ μμ μ€λ²λΌμ΄λ©μ μ°¨μ΄
ν¨μ μ¬μ μλ μλ μ μνλ ν¨μμ μ κ·Όμ΄ κ°λ₯νμ§λ§.
μ€λ²λΌμ΄λ©μ νλ©΄ μλ μ μνλ ν¨μμ μ‘΄μ¬κ°μ΄ μ¬λΌμ§κ² λλ€.
ν¨μ μ¬μ μλ μ μ λ°μΈλ©μΌλ‘ μ»΄νμΌ μκ°μ κ²°μ
μ€λ²λΌμ΄λ©μ λμ λ°μΈλ©μΌλ‘ μ€ν μκ°μ κ²°μ μ€λ²λΌμ΄λ© λͺ©μ
λΆλͺ¨ ν΄λμ€μμλ μΈν°νμ΄μ€λ₯Ό μ 곡νκ³ μμ ν΄λμ€μμ μ€λ²λΌμ΄λ©νμ¬ λ€νμ±μ μ€ννλ κ²
λμ λ°μΈλ©
λ°μΈλ© : λ³μμ νμ μ κ²°μ νλ― νλ‘κ·Έλ¨ κ΅¬μ± μμμ μ±κ²©μ κ²°μ ν΄μ£Όλ κ²
λμ λ°μΈλ© : μ»΄νμΌλ¬κ° κ°μν¨μλ₯Ό λ³΄κ³ μ°μ λμ΄κ° λ€μ λμ€μ μ€νμκ°μ κ°μν¨μλ₯Ό νΈμΆνκ² λλ©΄, κ°μ²΄ λ΄μ μ€λ²λΌμ΄λ©λ κ°μ ν¨μλ₯Ό λμ μΌλ‘ μ°Ύμ
νΈμΆνλ κ²λμ λ°μΈλ©μ΄ λ°μνλ κ²½μ°
- λΆλͺ¨ ν΄λμ€ λ΄μ λ©€λ² ν¨μκ° κ°μ ν¨μ νΈμΆ
- μμ ν΄λμ€ λ΄μ λ©€λ² ν¨μκ° κ°μ ν¨μ νΈμΆ
- μΈλΆ ν¨μμμ λΆλͺ¨ ν΄λμ€μ ν¬μΈν°λ‘ κ°μ ν¨μ νΈμΆ
- λ€λ₯Έ ν΄λμ€μμ κ°μ ν¨μ νΈμΆ
// λΆλͺ¨ ν΄λμ€ κ°μ λ©€λ² ν¨μ
virtual void draw();
// μμ ν΄λμ€
void drow();
// drawλ₯Ό drowλ‘ μλͺ»μ³€λ€. μ΄ κ²½μ° drowλΌλ λ©€λ² ν¨μλ‘ νΈμΆλμ΄ μ»΄νμΌλ¬κ° μ€λ₯ λ°μνμ§ μλλ€.
// override
// void drow() override;
// μ»΄νμΌλ¬κ° ν¨μκ° μ€λ²λΌμ΄λ©μ΄ λμ§ μμμΌλ―λ‘ μ€λ₯λ₯Ό μ‘μμ€λ€.
//final
//μμ λ° μ¬μ μ λ§λ μ§μμ΄, ν΄λμ€ μ΄λ¦, κ°μ ν¨μ μνμ λ€μ μμ±
class Student final{ // Studentν΄λμ€ μμ λΆκ°
virtual void show(); // showν¨μ μ€λ²λΌμ΄λ© λΆκ°
};
μ€λ²λΌμ΄λ©μ νΉμ§
- μ€λ²λΌμ΄λ© μ μ΄λ¦, 맀κ°λ³μ κ°μ νμ , 리ν΄νμ μ΄ μΌμΉν΄μΌνλ€.
- λΆλͺ¨ν΄λμ€μμλ virtualμ΄ μμ΄μΌ νμ§λ§, μμν΄λμ€μμλ virtual μλ΅ν΄λ λλ€.
- μ κ·Ό μ§μ μ μ¬μ© κ°λ₯
βοΈμ€λ²λΌμ΄λ©μ μν΄ μ‘΄μ¬κ°μ΄ μμ΄μ§ λΆλͺ¨ ν΄λμ€μ κ°μν¨μλ₯Ό μ¬μ©νκ³ μΆλ€λ©΄,
(::)λ₯Ό μ΄μ©νμ¬ μ μ λ°μΈλ©μΌλ‘ νΈμΆνλ©΄ λλ€.
κ°μμλ©Έμ
μ μμ μΈ μλ©Έμ μν΄μλ λΆλͺ¨ ν΄λμ€μ μλ©Έμλ₯Ό κ°μ μλ©Έμλ‘ κ΅¬ννλ κ²μ΄ μ’λ€.
μ μΊμ€ν μ ν ν μλ©Έμλ₯Ό λΆλ¬μ€κ² λλ©΄ λΆλͺ¨ν΄λμ€μ μλ©Έμλ§ λΆλ¬μμ§λ€.
λλ¬Έμ, κ°μμλ©Έμλ‘ κ΅¬ννμ¬ μμ ν΄λμ€μ μλ©Έμλ₯Ό λμ λ°μΈλ©μΌλ‘ λΆλ¬μ¨λ€.
μ΄μ§νΌ, μμ ν΄λμ€κ° μλ©Έλλ©΄ λΆλͺ¨ ν΄λμ€μ μλ©Έμλ μλμΌλ‘ μ€νλλ€.
μμ κ°μ ν¨μ
μ΄μ§νΌ μΈν°νμ΄μ€μ μν μ΄λ―λ‘,
ν¨μμ μ½λκ° μκ³ μ μΈλ§ μλ κ°μν¨μλ₯Ό λ§νλ€.
λ°λΌμ λΆλͺ¨ ν΄λμ€μμ ꡬνμ ν΄μλ μλλ€.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;
}
}
}