[C++] 명품 C++ 프로그래밍 8장 실습문제 풀이
8장 상속 실습문제
1번
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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() {
NamedCircle waffle(3, "waffle");
waffle.show();
}
2번
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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 = 0, string name = "")
: Circle(radius) {
this->name = name;
}
void setName(string name) { this->name = name; }
string getName() { return name; }
void show() { cout << "반지름이 " << getRadius() << "인 " << name << endl; }
};
int main() {
NamedCircle pizza[5];
int radius;
string name;
int maxAreaCircleIndex = 0;
int maxRadius = INT_MIN;
cout << "5개의 정수 반지름과 원의 이름을 입력하세요" << endl;
for (int i = 0; i < 5; i++) {
cout << i + 1 << " >> ";
cin >> radius >> name;
pizza[i].setRadius(radius);
pizza[i].setName(name);
if (maxRadius < radius) {
maxRadius = radius;
maxAreaCircleIndex = i;
}
}
cout << "가장 면적이 큰 피자는 " << pizza[maxAreaCircleIndex].getName() <<"입니다" << endl;
}
3번
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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 = "")
: Point(x, y) {
this->color = color;
}
void setPoint(int x, int y) { move(x, y); }
void setColor(string color) { this->color = color; }
string getColor() { return 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번
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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; }
string getColor() { return color; }
void show() {
cout << color << "색으로 (" << getX() << ',' << getY() << ")에 위치한 점입니다." << endl;
}
};
int main() {
ColorPoint zeroPoint;
zeroPoint.show();
ColorPoint cp(5, 5);
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
}
5번
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
class BaseArray {
private:
int capacity;
int* mem;
protected:
BaseArray(int capacity = 100) {
this->capacity = capacity;
this->mem = new int[capacity];
}
~BaseArray() { if (mem) delete[]mem; }
void put(int index, int val) { mem[index] = val; }
int get(int index) { return mem[index]; }
int getCapacity() { return capacity; }
};
class MyQueue : public BaseArray {
int front, rear;
public:
MyQueue(int Capacity = 100)
: BaseArray(Capacity) {
this->front = this->rear = 0;
}
void enqueue(int n) { put(rear++, n); }
int dequeue() { return get(front++); }
int length() { return rear - front; }
int capacity() { return getCapacity(); }
};
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번
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
class BaseArray {
private:
int capacity;
int* mem;
protected:
BaseArray(int capacity = 100) {
this->capacity = capacity;
this->mem = new int[capacity];
}
~BaseArray() { if (mem) delete[]mem; }
void put(int index, int val) { mem[index] = val; }
int get(int index) { return mem[index]; }
int getCapacity() { return capacity; }
};
class MyStack : public BaseArray {
int top;
public:
MyStack(int Capacity = 100)
: BaseArray(Capacity) {
this->top = -1;;
}
void push(int n) { put(++top, n); }
int pop() { return get(top--); }
int length() { return top + 1; }
int capacity() { return getCapacity(); }
};
int main() {
MyStack mStack(100);
int n;
cout << "스택에 삽입할 5개의 정수를 입력하라>> ";
for (int i = 0; i < 5; i++) {
cin >> n;
mStack.push(n);
}
cout << "스택의 용량: " << mStack.capacity() << ", 스택의 크기 : " << mStack.length() << endl;
cout << "스택의 원소를 순서대로 제거하여 출력한다 >> ";
while (mStack.length() != 0)
cout << mStack.pop() << ' ';
cout << endl << "스택의 현재 크기 : " << mStack.length() << endl;
}
7번
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;
class BaseMemory {
char* mem;
protected:
BaseMemory(int size) { mem = new char[size]; }
void set(int index, char data) { mem[index] = data; }
char get(int index) { return mem[index]; }
};
class ROM : public BaseMemory {
void write(int index, char data);
public:
ROM(int size, char data[], int length)
: BaseMemory(size) {
for (int i = 0; i < length; i++) {
set(i, data[i]);
}
}
char read(int index) { return get(index); }
};
class RAM : public BaseMemory {
public:
RAM(int size) : BaseMemory(size) {}
void write(int index, char data) { set(index, data); }
char read(int index) { return get(index); }
};
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번
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <string>
using namespace std;
class Printer {
string model, manufacturer;
int printedCount, availableCount;
public:
Printer(string model, string manufacturer, int availableCount) {
this->model = model;
this->manufacturer = manufacturer;
this->availableCount = availableCount;
}
bool isAvailToPrint(int count) {
if (availableCount < count) {
cout << "용지가 부족하여 프린트할 수 없습니다" << endl;
return false;
}
else
return true;
}
void print(int count) {
availableCount -= count;
printedCount += count;
}
void show() {
cout << model << ", " << manufacturer << ", 남은종이 " << availableCount << "장, ";
}
};
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 count) {
if (availableInk < count) {
cout << "잉크가 부족하여 프린트할 수 없습니다." << endl;
return;
}
if (Printer::isAvailToPrint(count)) {
Printer::print(count);
availableInk -= count;
cout << "프린트하였습니다." << endl;
}
}
void show() {
cout << "잉크젯 : ";
Printer::show();
cout << "남은 잉크 " << availableInk << endl;
}
};
class LaserPrinter : public Printer {
int availableTonner;
public:
LaserPrinter(string model, string manufacturer, int availableCount, int availableTonner)
: Printer(model, manufacturer, availableCount) {
this->availableTonner = availableTonner;
}
void print(int count) {
if (availableTonner < count) {
cout << "토너가 부족하여 프린트할 수 없습니다." << endl;
return;
}
if (Printer::isAvailToPrint(count)) {
Printer::print(count);
availableTonner -= count;
cout << "프린트하였습니다." << endl;
}
}
void show() {
cout << "레이저 : ";
Printer::show();
cout << "남은 토너 " << availableTonner << endl;
}
};
class PrinterManager {
public:
void run() {
InkJetPrinter IP("Officejet V40", "HP", 5, 10);
LaserPrinter LP("SCX-6x45", "삼성전자", 3, 20);
cout << "현재 작동중인 2대의 프린터는 아래와 같다" << endl;
IP.show();
LP.show();
int selectPrinter, printCount;
char cont;
while (true) {
cout << endl;
cout << "프린터(1:잉크젯, 2:레이저)와 매수 입력 >> ";
cin >> selectPrinter >> printCount;
switch (selectPrinter) {
case 1:
IP.print(printCount);
break;
case 2:
LP.print(printCount);
break;
}
IP.show();
LP.show();
cout << "계속 프린트 하시겠습니까?(y/n) >> ";
cin >> cont;
if (cont == 'n')
break;
}
}
};
int main() {
PrinterManager PM;
PM.run();
}
9번
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <string>
using namespace std;
class Seat {
string name;
public:
Seat(string name = "---") { this->name = name; }
void setName(string name) { this->name = name; }
string getName() { return name; }
};
class Schedule {
int seatSize;
string time;
Seat* seat;
public:
Schedule(string time = "") {
this->time = time;
this->seatSize = 8;
seat = new Seat[this->seatSize];
}
void setTime(string time) { this->time = time; }
string getTime() { return time; }
void book(int index, string name) { seat[index].setName(name); }
void cancelBook(int index) { seat[index].setName("---"); }
void show() {
cout << time;
for (int i = 0; i < seatSize; i++)
cout << '\t' << seat[i].getName();
cout << endl;
}
};
class Console {
public:
static int getMenu() {
int select;
cout << "예약:1, 취소:2, 보기:3, 끝내기:4 >> ";
cin >> select;
cin.ignore();
return select;
}
static int getTime() {
int select;
cout << "07시:1, 12시:2, 17시:3 >> ";
cin >> select;
cin.ignore();
return select - 1;
}
static int getSeatNum() {
int select;
cout << "좌석번호 >> "; cin >> select;
cin.ignore();
return select - 1;
}
static string getName() {
string name;
cout << "이름 입력 >> "; getline(cin, name);
return name;
}
};
class AirlineBook {
Schedule* schedule;
int scheduleSize;
public:
AirlineBook() {
this->scheduleSize = 3;
this->schedule = new Schedule[this->scheduleSize];
schedule[0].setTime("07시");
schedule[1].setTime("12시");
schedule[2].setTime("17시");
}
void book(int time, int index, string name) { schedule[time].book(index, name); }
void cancel(int time, int index) { schedule[time].cancelBook(index); }
void show(int time) { schedule[time].show(); }
};
class AirlineBookManager {
AirlineBook* AB;
int seatNum;
string name;
void book() {
int time = Console::getTime();
AB->show(time);
seatNum = Console::getSeatNum();
name = Console::getName();
AB->book(time, seatNum, name);
}
void cancel() {
int time = Console::getTime();
AB->show(time);
seatNum = Console::getSeatNum();
name = Console::getName();
AB->cancel(time, seatNum);
}
void show() {
for (int i = 0; i < 3; i++)
AB->show(i);
}
public:
AirlineBookManager() { AB = new AirlineBook; }
void run() {
cout << "***** 한성항공에 오신것을 환영합니다. *****" << endl;
int flag = true;
while (flag) {
switch (Console::getMenu()) {
case 1:
book();
break;
case 2:
cancel();
break;
case 3:
show();
break;
case 4:
flag = false;
break;
default:
cout << "다시 입력하세요" << endl;
break;
}
cout << endl;
}
cout << "예약 시스템을 종료합니다." << endl;
}
};
int main() {
AirlineBookManager ABM;
ABM.run();
}
This post is licensed under CC BY 4.0 by the author.