Computer Security

#4 C++ 클래스 상속 본문

프로그래밍/C++

#4 C++ 클래스 상속

쿠리 Kuri 2022. 6. 15. 18:30

상속
-상속(Inheritance)은 객체 지향 프로그래밍의 주요한 특성 중 하나다.

-현실 세계에서의 상속의 개념을 프로그래밍으로 그대로 가져와 사용할 수 있다.

-이를 통해 프로그램의 논리적 구조를 계층적으로 구성할 수 있다.
-흔히 자식이 부모의 속성을 물려 받듯이, 자식 클래스(Child Class)가 부모 클래스(Parent Class)의 속성을 그대로 물려

 받아 사용할 수 있다.

-그러므로 상속을 활용하여 소스코드의 재사용성을 늘릴 수 있다.

 


자식 클래스는 파생 클래스(Derived Class)라고도 불리우며, 부모 클래스(Parent Class)의 모든 속성을 물려 받는다.

자식 클래스는 콜론(:)을 활용하여 부모 클래스와 연결될 수 있다.

 

코드 예시

#include <iostream>
#include <string>

using namespace std;

class Person {
private:
	string name;
public:
	Person(string name) : name(name) { }
	string getName() {
		return name;
	}
	void showName() {
		cout << "이름: " << getName() << '\n';
	}
};

class Student : Person {  //student가 : 을 통해 Person부모 클래스 상속받음
private:
	int studentID;
    //멤버변수 name은 Person에게 물려받음
public:
	Student(int studentID, string name) : Person(name) {
		this->studentID = studentID;
	}
	void show() {
		cout << "학생 번호: " << studentID << '\n';
		cout << "학생 이름: " << getName() << '\n';
	}
};
int main(void) {
	Student student(1, "고동산");
	student.show();
	system("pause");
}

 

결과 값

 

-자식 클래스의 인스턴스를 만들 때 가장 먼저 부모 클래스의 생성자가 호출된다.

-이후에 자식 클래스의 생성자가 호출된다.

-또한 자식 클래스의 수명이 다했을 때는 자식 클래스의 소멸자가 먼저 호출된 이후에 부모 클래스의 소멸자가 호출된다.


오버라이딩
-부모 클래스에서 정의된 함수를 무시하고, 자식 클래스에서 동일한 이름의 함수를 재정의하는 문법이다.

-오버라이딩을 적용한 함수의 원형은 기존의 함수와 동일한 매개변수를 전달 받는다.

 

코드 예시

#include <iostream>
#include <string>

using namespace std;

class Person {
private:
	string name;
public:
	Person(string name) : name(name) { }
	string getName() {
		return name;
	}
	void showName() {
		cout << "이름: " << getName() << '\n';
	}
};

class Student : Person {
private:
	int studentID;
public:
	Student(int studentID, string name) : Person(name) {
		this->studentID = studentID;
	}
	void show() {
		cout << "학생 번호: " << studentID << '\n';
			cout << "학생 이름: " << getName() << '\n';
	}
	void showName() {
		cout << "학생 이름: " << getName() << '\n';
	}
};
int main(void) {
	Student student(1, "고동산");
	student.showName();
	system("pause");
}

결과 값

같은이름이 재정의되면, 자식클래스의 함수대로 따라간다.


다중 상속
다중 상속(Multipple Inheritance)는 여러 개의 클래스로부터 멤버를 상속 받는 것을 말한다.

 

부모 클래스 하나 더 정의하기

ex)

class Temp {
public:
void showTemp() {
cout << "임시 부모 클래스입니다.\n";
}
};

 

다중 상속: 자식 클래스 사용해보기

 

코드 예시

#include <iostream>
#include <string>

using namespace std;


class Temp {
public:
	void showTemp() {
		cout << "임시 부모 클래스입니다.\n";
	}
};
class Person {
private:
	string name;
public:
	Person(string name) : name(name) { }
	string getName() {
		return name;
	}
	void showName() {
		cout << "이름: " << getName() << '\n';
	}
};

class Student : Person, public Temp {
private:
	int studentID;
public:
	Student(int studentID, string name) : Person(name) {
		this->studentID = studentID;
	}
	void show() {
		cout << "학생 번호: " << studentID << '\n';
			cout << "학생 이름: " << getName() << '\n';
	}
	void showName() {
		cout << "학생 이름: " << getName() << '\n';
	}

};

int main(void) {
	Student student(1, "고동산");
	student.showName();
	student.showTemp();
	system("pause");
}

결과 값

 

다중 상속의 한계
1) 여러 개의 부모 클래스에 동일한 멤버가 존재할 수 있다.
2) 하나의 클래스를 의도치 않게 여러 번 상속받을 가능성이 있다


이렇듯, 상속의 원리를 활용해 소스코드의 재사용성을 증대 시킬 수 있다.

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

#6 C++ 프렌드  (0) 2022.06.17
#5 C++ 오버로딩  (0) 2022.06.16
#3 C++ 생성자와 소멸자  (0) 2022.06.14
#2 C++의 클래스  (0) 2022.06.13
#1 C언어와 C++비교하기  (0) 2022.06.12
Comments