-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek11.cpp
More file actions
119 lines (101 loc) · 2.16 KB
/
week11.cpp
File metadata and controls
119 lines (101 loc) · 2.16 KB
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
// 예제 8-1
#include <iostream>
#include <string>
using namespace std;
class Point {
int x, y;
public:
void set(int x, int y) { this->x = x; this->y = y;}
void showPoint(){
cout << "(" << x << "," << y << ")" << endl;
}
};
class ColorPoint : public Point {
string color;
public:
void setColor(string color) { this->color = color; }
void showColorPoint();
};
void ColorPoint::showColorPoint() {
cout << color << ":";
showPoint();
}
int main() {
Point p;
ColorPoint cp;
cp.set(3,4);
cp.setColor("Red");
cp.showColorPoint();
}
// 실행 결과
Red:(3,4)
// 예제 8-3
#include <iostream>
#include <string>
using namespace std;
class TV {
int size;
public:
TV() { size = 20; }
TV(int size) { this-> size = size; }
int getSize() { return size; }
};
class WideTV : public TV {
bool videoIn;
public:
WideTV(int size, bool videoIn) : TV(size) {
this->videoIn = videoIn;
}
bool getVideoIn() { return videoIn; }
};
class SmartTV : public WideTV {
string ipAddr;
public:
SmartTV(string ipAddr, int size) : WideTV(size, true) {
this->ipAddr = ipAddr;
}
string getAddr() { return ipAddr; }
};
int main() {
SmartTV htv("192.0.0.1", 32);
cout << "size=" << htv.getSize() << endl;
cout << "videoIn=" << boolalpha << htv.getVideoIn() << endl;
cout << "IP=" << htv.getAddr() << endl;
}
// 실행 결과
size=32
videoIn=true
IP=192.0.0.1
// 예제 8-7
#include <iostream>
using namespace std;
class Adder {
protected:
int add(int a, int b) { return a+b; }
};
class Subtractor {
protected:
int minus(int a, int b) { return a-b; }
};
class Calculator : public Adder, public Subtractor {
public:
int calc(char op, int a, int b);
};
int Calculator::calc(char op, int a, int b) {
int res=0;
switch(op) {
case '+' : res = add(a, b); break;
case '-' : res = minus(a, b); break;
}
return res;
}
int main() {
Calculator handCalculator;
cout << "2 + 4 = "
<< handCalculator.calc('+', 2, 4) << endl;
cout << "100 - 8 = "
<< handCalculator.calc('-', 100, 8) << endl;
}
// 실행 결과
2 + 4 = 6
100 - 8 = 92