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;
}