C++ 10주차 연산자 중복과 상속
단항 연산자 중복
정의
- 피연산자가 1개인 연산자
종류
- 전위 연산자 * ++val, –val
- 후위 연산자
- val++, v–
전위 ++ 연산자 중복
- 객체에 매개변수 없이 ++ 함수를 호출하는 것과 동일함
예시 코드
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
#include <iostream>
using namespace std;
class Circle {
int radius, price;
public:
Circle(int radius=0, int price = 0) { this->radius = radius; this->price = price; }
void show();
Circle& operator++(); //++ 연산자 함수 선언
};
void Circle::show() { cout << "radius=" << radius << ',' << "price=" << price << endl; }
Circle& Circle::operator++() {
radius++;
price++;
return *this;
}
int main()
{
Circle a(1, 2), b;
a.show();
b.show();
b = ++a;
a.show();
b.show();
}
1
2
3
4
5
6
Output :
radius=1,price=2
radius=0,price=0
radius=2,price=3
radius=2,price=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
class Circle2 {
int radius;
int price;
public:
Circle2(int radius = 0, int price = 0) { this->radius = radius; this->price = price; }
void show();
bool operator! (); //!연산자 중복
};
void Circle2::show() { cout << "radius=" << radius << ',' << "price=" << price << endl; }
bool Circle2::operator!() {
if (radius == 0 && price == 0) return true;
else return false;
}
int main()
{
Circle2 a(0, 0), b(5, 5);
if (!a) cout << "원 a의 크기와 가격이 0" << endl;
else cout << "원 a의 크기와 가격이 0이 아님" << endl;
if (!b) cout << "원 b의 크기와 가격이 0" << endl;
else cout << "원 b의 크기와 가격이 0이 아님" << endl;
}
1
2
3
4
Output:
원 a의 크기와 가격이 0
원 b의 크기와 가격이 0이 아님
후위 ++ 연산자 중복
- 전위 ++과의 비교를 위해 임의의 정수를 사용해 함수 중복 구현
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Circle3 {
int radius, price;
public:
Circle3(int radius = 0, int price = 0) { this->radius = radius; this->price = price; }
void show();
Circle3 operator++ (int x); //후위 ++ 연산자 함수 선언
};
void Circle3::show() { cout << "radius=" << radius << ',' << "price=" << price << endl; }
Circle3 Circle3::operator++(int x) { Circle3 tmp = *this; //증가 이전 객체 상태 저장
radius++;
price++;
return tmp; //증가 이전 객체 상태 리턴
}
int main(){
Circle3 a(3, 5), b;
a.show();
b.show();
b = a++;
a.show();
b.show();
}
1
2
3
4
5
6
Output:
radius=3,price=5
radius=0,price=0
radius=4,price=6
radius=3,price=5
Friend를 이용한 연산자 중복
+ 연산자 중복
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Circle4 {
int radius, price;
public:
Circle4(int radius = 0, int price = 0) { this->radius = radius; this->price = price; }
void show();
friend Circle4 operator+ (Circle4 op1, Circle4 op2); //프렌드 선언
};
void Circle4::show() { cout << "radius=" << radius << ',' << "price=" << price << endl; }
Circle4 operator+(Circle4 op1, Circle4 op2) {
Circle4 tmp; //임시 객체 생성
tmp.radius = op1.radius + op2.radius; //radius 더하기
tmp.price = op1.price + op2.price; //price 더하기
return tmp; //임시 객체 리턴
}
int main(){
Circle4 a(3, 5), b(4, 6), c;
c = a + b;
a.show();
b.show();
c.show();
}
1
2
3
4
5
Output:
radius=3,price=5
radius=4,price=6
radius=7,price=11
1
2
3
4
5
6
7
8
9
int main()
{
Circle5 a(3, 5), b;
a.show();
b.show();
b = 2 + a;
a.show();
b.show();
}
1
2
3
4
5
6
Output:
radius=3,price=5
radius=0,price=0
radius=3,price=5
radius=5,price=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
class Circle6 {
int radius;
int price;
public:
Circle6(int radius = 0, int price = 0) {
this->radius = radius; this->price = price;
}
void show();
friend Circle6& operator++(Circle6& op);
friend Circle6 operator++(Circle6& op, int x);
};
void Circle6::show() {
cout << "radius=" << radius << ',' << "price=" << price << endl;
}
Circle6& operator++(Circle6& op) {// 전위 연산자
op.radius++;
op.price++;
return op; // 연산 결과 리턴
}
Circle6 operator++(Circle6& op, int x) {// 후위 연산자
Circle6 tmp = op; // 변경하기 전의 op 상태 저장
op.radius++;
op.price++;
return tmp; // 변경 이전의 op 리턴
}
int main(){
Circle6 a(3, 5), b;
b = ++a; //전위 ++ 연산자
a.show();
b.show();
b = a++; //후위 ++ 연산자
a.show();
b.show();
}
1
2
3
4
5
6
Output:
radius=4,price=6
radius=4,price=6
radius=5,price=7
radius=4,price=6
참조 반환하는 연산자 중복
« 연산자 중복
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Circle7 {
int radius, price;
public:
Circle7(int radius = 0, int price = 0) { this->radius = radius; this->price = price; }
void show();
Circle7& operator << (int n);
};
void Circle7::show() { cout << "radius=" << radius << ',' << "price=" << price << endl; }
Circle7& Circle7::operator<< (int n) {
radius += n;
price += n;
cout << "inputed value : " << n << endl;
return *this; //이 객체의 참조 리턴
}
int main(){
Circle7 a(1, 2);
a << 3 << 5 << 6;
a.show();
}
1
2
3
4
5
6
Output:
inputed value : 3
inputed value : 5
inputed value : 6
radius=15,price=16
상속
개념
- 물질적인 상속, 유전적인 상속
- C++에서의 상속
- 기본 클래스의 속성, 기능을 파생 클래스에 물려주는 것
- 기본 클래스(base class): 부모 클래스, 상속해주는 클래스임
- 파생 클래스(derived class) : 자식 클래스, 상속을 받는 클래스임
- 다중 상속
- 여러 클래스를 상속 받는 기능(C++에서만 가능)
- 코드의 재활용성 상승
- 기본 클래스의 속성, 기능을 파생 클래스에 물려주는 것
상속의 장점
모듈화
- 직관적 단위의 모듈 별로 설계 및 구현이 가능함
- 상속을 통해 간결한 설계가 가능
계층적 분류
- 구조적 설계 용이
생산성 향상
- 재사용성 향상으로 빠른 생산이 가능함
형식
1 2 3 4
class 파생클래스명 : 접근지정자 기본 클래스명 ex) class MultimediaTv : public TV
예시 코드
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
class Shape1 { int x, y; //한 점 (x,y)의 좌표값 int type; //0: 미지정 1: 원 2: 사각형 public: Shape1() { type = 0; } void set(int x, int y) { this->x = x; this->y = y; } void showPoint() { cout << '(' << x << ',' << y << ')' << endl; } }; class Circle_Inheritance : public Shape1 { string color; public: void setColor(string color) { this->color = color; } void showCircle(); }; void Circle_Inheritance::showCircle() { cout << color << ':'; showPoint(); //Shape1의 showPoint 호출 } int main() { Shape1 p; //기본 클래스의 객체 생성 Circle_Inheritance cp; //파생 클래스의 객체 생성 cp.set(3, 4); //기본 클래스의 멤버 호출 cp.setColor("Red"); //파생 클래스의 멤버 호출 cp.showCircle(); }
1 2
Output : Red:(3,4)
클래스 다이어그램
객체지향 설계를 위한 도구
- 상세설계서에서 명사구, 동사구를 구별해 클래스변수, 함수를 찾음
사용방법
classDiagram class 기본 클래스 기본 클래스 : 멤버변수 이름 기본 클래스 : 멤버함수 이름 class 파생 클래스 파생 클래스 : 멤버변수 이름 파생 클래스 : 멤버함수 이름 기본 클래스 <|-- 파생 클래스예시
1 2 3 4 5 6 7 8 9 10 11
class TV { int channel; public void showChannel(); } class MultiMediaTV : public TV{ movie; public: void playMovie(); }
- 클래스 다이어그램으로 구현
classDiagram class TV TV : channel TV : showChannel class MultiMediaTV MultiMediaTV : movie MultiMediaTV : playMovie TV <|-- MultiMediaTV
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.