프로그래밍/C++

상속

swedu 2023. 2. 23. 10:20
728x90
반응형

2023.02.23  최초 작성

 

 

 

공통된 내용을 여러 클래스가 중복해서 가지지 않고 부모 클래스로 만들어서 정의하고 자식 클래스가 상속받아 사용할 수 있다.

 

형식은 아래와 같다.

 

 

class 부모클래스 {

 

}

class 자식클래스 : 접근제한자 부모클래스

{

 

}

 

 

상속받을 때 지정한 접근제한자보다 접근 범위가 넓은 것은 접근권한이 축소된다.

 

자식 클래스에서 부모의 멤버를 접근할 때는 __super을 이용할 수 있다.

 

부모 클래스에서 물려받은 함수를 자식 클래스가 재정의(오버라이딩)할 수 있다.

 

둘 이상의 부모 클래스로부터 다중 상속받을 수 있다.

 

 

#include <iostream>
using namespace std;
class ClassA {
private:
	int a;
protected:
	int b;
public:
	int c;
	ClassA(int a, int b, int c) {
		cout << "ClassA 생성자 실행" << endl;
		this->a = a;
		this->b = b;
		this->c = c;
	}
	void print() {
		cout << "ClassA print 실행" << endl;
		cout << "a : " << a << endl;
		cout << "b : " << b << endl;
		cout << "c : " << c << endl;
	}
	void method01() {
		cout << "ClassA method01 실행" << endl;
	}
};
class ClassA2 {
private:
	int a2;
protected:
	int b2;
public:
	int c2;
	ClassA2(int a2, int b2, int c2) :
		a2(a2), b2(b2), c2(c2) {
		cout << "ClassA2 생성자 실행" << endl;
	}
	void print2() {
		cout << "ClassA2 print2 실행" << endl;
		cout << "a2 : " << a2 << endl;
		cout << "b2 : " << b2 << endl;
		cout << "c2 : " << c2 << endl;
	}
	void method02() {
		cout << "ClassA2 method02 실행" << endl;
	}
};
class ClassB :public ClassA, ClassA2 {
public:
	ClassB(int a, int b, int c):ClassA(a, b, c), ClassA2(a, b, c){
		cout << "ClassB 생성자 실행" << endl;
	}
	void print() {
		cout << "ClassB print 실행" << endl;
		//cout << "a : " << a << endl;
		cout << "b : " << b << endl;
		cout << "c : " << c << endl;
		__super::print();
	}
	void print2() {
		cout << "ClassB print2 실행" << endl;
		//cout << "a2 : " << a2 << endl;
		cout << "b2 : " << b2 << endl;
		cout << "c2 : " << c2 << endl;
		__super::print2();
	}
	void method01() {
		cout << "ClassB method01 실행" << endl;
		__super::method01();
	}
	void method02() {
		cout << "ClassB method02 실행" << endl;
		__super::method02();
	}
};
int main() {
	ClassB b1(100, 200, 300);
	b1.print();
	b1.print2();
	b1.method01();
	b1.method02();
	return EXIT_SUCCESS;
}

 

[실행 결과]

ClassA 생성자 실행

ClassA2 생성자 실행

ClassB 생성자 실행

ClassB print 실행

b : 200

c : 300

ClassA print 실행

a : 100

b : 200

c : 300

ClassB print2 실행

b2 : 200

c2 : 300

ClassA2 print2 실행

a2 : 100

b2 : 200

c2 : 300

ClassB method01 실행

ClassA method01 실행

ClassB method02 실행

ClassA2 method02 실행

 
#include <iostream>
using namespace std;

class Car {
private:
	int price;
protected:
	char* color;
	int seat;
	void setPrice(int price) {
		cout << "Car클래스의 setPrice 함수 실행" << endl;
		this->price = price;
	}
public:
	Car(int price, char* color, int seat) :
		price(price), color(color), seat(seat) {
		cout << "Car(int price, char* color, int seat) 생성자 실행" << endl;
	}
	Car(char* color, int seat) :Car(0, color, seat) {
		cout << "Car(char* color, int seat) 생성자 실행" << endl;
	}
	~Car() {
		cout << "Car 소멸자 실행" << endl;
	}
	void print() {
		cout << "색상 : " << this->color << endl;
		cout << "좌석수 : " << this->seat << endl;
		if (!this->price) {
			cout << "가격 : " << "미정" << endl;
		}
		else {
			cout << "가격 : " << this->price << endl;
		}
	}

};
class Sedan : public Car {
private:
	char* model;
public:
	Sedan(char* model, char* color, int seat) :Car(color, seat), model(model) {
		cout << "Sedan 생성자 실행" << endl;
	}
	~Sedan() {
		cout << "Sedan 소멸자 실행" << endl;
	}
	void setPrice(int price) {
		cout << "Sedan클래스의 setPrice 함수 실행" << endl;
		__super::setPrice(price);
	}
	void setColor(char* color) {
		__super::color = color;
	}
	void setSeat(int seat) {
		__super::seat = seat;
	}
	void print() {
		cout << "[차량 정보]" << endl;
		cout << "모델명 : " << model << endl;
		cout << "차량 종류 : 세단" << endl;
		__super::print();
	}
};
class Truck : public Car {
private:
	char* model;
public:
	Truck(char* model, char* color, int seat) :Car(color, seat), model(model) {
		cout << "Truck 생성자 실행" << endl;
	}
	~Truck() {
		cout << "Truck 소멸자 실행" << endl;
	}
	void setPrice(int price) {
		cout << "Truck클래스의 setPrice 함수 실행" << endl;
		__super::setPrice(price);
	}
	void setColor(char* color) {
		__super::color = color;
	}
	void setSeat(int seat) {
		__super::seat = seat;
	}
	void print() {
		cout << "[차량 정보]" << endl;
		cout << "모델명 : " << model << endl;
		cout << "차량 종류 : 트럭" << endl;
		__super::print();
	}
};
int main() {
	char color[]("white");
	char color2[]("black");
	char model[]("K5");
	Sedan s1(model, color, 4);
	s1.print();
	s1.setPrice(3000);
	s1.setColor(color2);
	s1.setSeat(5);
	s1.print();
	char model2[]("Porter");
	Truck t1(model2, color, 2);
	t1.print();
	t1.setPrice(2000);
	t1.print();
	return EXIT_SUCCESS;
}

[실행 결과]

Car(int price, char* color, int seat) 생성자 실행
Car(char* color, int seat) 생성자 실행
Sedan 생성자 실행
[차량 정보]
모델명 : K5
차량 종류 : 세단
색상 : white
좌석수 : 4
가격 : 미정
Sedan클래스의 setPrice 함수 실행
Car클래스의 setPrice 함수 실행
[차량 정보]
모델명 : K5
차량 종류 : 세단
색상 : black
좌석수 : 5
가격 : 3000
Car(int price, char* color, int seat) 생성자 실행
Car(char* color, int seat) 생성자 실행
Truck 생성자 실행
[차량 정보]
모델명 : Porter
차량 종류 : 트럭
색상 : white
좌석수 : 2
가격 : 미정
Truck클래스의 setPrice 함수 실행
Car클래스의 setPrice 함수 실행
[차량 정보]
모델명 : Porter
차량 종류 : 트럭
색상 : white
좌석수 : 2
가격 : 2000
Truck 소멸자 실행
Car 소멸자 실행
Sedan 소멸자 실행
Car 소멸자 실행

 
 
728x90
반응형
 
 

 

728x90
반응형

'프로그래밍 > C++' 카테고리의 다른 글

vector  (0) 2023.02.27
가상함수, 순수가상함수, 추상클래스  (0) 2023.02.24
연산자 중복  (0) 2023.02.21
프랜드  (0) 2023.02.21
static 수명이 길다  (0) 2023.02.21