STUDY BLOG
명품 C++ programming Chapter 08 예제 본문
명품 C++ programming Chapter 08 예제
예제 8-1 Point 클래스를 상속받는 ColorPoint 클래스 만들기
#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();
}
예제 8-2 protected 멤버에 대한 접근
#include <iostream>
#include <string>
using namespace std;
class Point {
protected:
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();
bool equals(ColorPoint p);
};
void ColorPoint::showColorPoint() {
cout << color << ":";
showPoint();
}
bool ColorPoint::equals(ColorPoint p) {
if(x == p.x && y == p.y && color == p.color)
return true;
else
return false;
}
int main() {
Point p;
p.set(2,3);
p.x=5;
p.y=5;
p.showPoint();
ColorPoint cp;
cp.x=10;
cp.y=10;
cp.set(3,4);
cp.setColor("Red");
ColorPoint cp2;
cp2.set(3,4);
cp2.setColor("Red");
cout << ((cp.equals(cp2))?"true":"false");
}
예제 8-3 TV, WideTV, SmartTV의 상속 관계와 생성자 매개 변수 전달
#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 getIpAddr() { return ipAddr; }
};
int main() {
SmartTV htv("192.0.0.1", 32);
cout << "size =" << htv.getSize() << endl;
cout << "videoIn =" << boolalpha << htv.getVideoIn() << endl;
cout << "OP =" << htv.getIpAddr() << endl;
}
예제 8-4 private 상속 사례
#include <iostream>
using namespace std;
class Base {
int a;
protected:
void setA(int a) { this->a = a; }
public:
void showA() { cout << a; }
};
class Derived : private Base {
int b;
protected:
void setB(int b) { this->b = b; }
public:
void showB() { cout << b; }
};
int main() {
Derived x;
x.a=5; //컴파일 오류
x.setA(10); //컴파일 오류
x.showA(); //컴파일 오류
x.b=10; //컴파일 오류
x.setB(10); //컴파일 오류
x.showB();
}
예제 8-5 protected 상속 사례
#include <iostream>
using namespace std;
class Base {
int a;
protected:
void setA(int a) { this->a = a; }
public:
void showA() { cout << a; }
};
class Derived : protected Base {
int b;
protected:
void setB(int b) { this->b = b; }
public:
void showB() { cout << b; }
};
int main() {
Derived x;
x.a=5; //컴파일 오류
x.setA(10); //컴파일 오류
x.showA(); //컴파일 오류
x.b=10; //컴파일 오류
x.setB(10); //컴파일 오류
x.showB();
}
예제 8-6 상속이 중첩될 때 접근 지정 사례
#include <iostream>
using namespace std;
class Base {
int a;
protected:
void setA(int a) { this->a = a; }
public:
void showA() { cout << a; }
};
class Derived : private Base {
int b;
protected:
void setB(int b) { this->b = b; }
public:
void showB() {
setA(5);
showA();
cout << b;
}
};
class GrandDerived : private Derived {
int c;
protected:
void setAB(int x) {
setA(x); //컴파일 오류
showA(); //컴파일 오류
set(x);
}
};
예제 8-7 Adder와 Subtractor를 상속받는 Calculator 클래스 작성
#include <iostream>
using namespace std;
class Adder {
protected:
int add(int a, int b) { return a+b; }
};
class Substractor {
protected:
int minus(int a, int b) { return a-b; }
};
class Calculator : public Adder, public Subastractor {
public:
int clac(char op, int a, int b);
};
int Calculator::clac(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.clac('+',2,4) << endl;
cout << "100-8 = " << handCalculator.clac('-',100,8) << endl;
}
'Software > C++' 카테고리의 다른 글
명품 C++ programming Chapter 09 예제 (0) | 2020.12.05 |
---|---|
명품 C++ programming Chapter 07 예제 (0) | 2020.11.25 |
명품 C++ programming Chapter 06 예제 (0) | 2020.11.22 |
명품 C++ programming Chapter 05 예제 (0) | 2020.11.20 |
명품 C++ programming Chapter 04 예제 (1) | 2020.11.02 |