Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 오류
- String
- W3Schools
- Programming
- 파이썬
- 기초
- 재귀
- Unreal Engine 5
- c++
- Class
- Basic
- loop
- dynamic
- 시작해요 언리얼 2022
- C#
- Material
- DP
- w3school
- github
- Algorithm
- 백준
- guide
- Unity
- UE5
- 문제풀이
- parameter
- 프로그래밍
- dfs
- python
- Tutorial
Archives
- Today
- Total
행복한 개구리
C++ Classes - Inheritance / Inheritance 본문

상속
C++에서는 한 클래스에서 다른 클래스로 속성과 메서드들을 상속하는 것이 가능합니다. 우리는 "상속 개념"을 두 카테고리로 묶었습니다:
- 파생 클래스(자식, child) - 다른 클래스를 상속받는 클래스
- 기반 클래스(부모, parent) - 상속의 뿌리가 되는 클래스
클래스를 상속시키려면 : 표시를 사용하세요.
아래 예시에서, Car 클래스(자식)는 Vehicle 클래스(부모)로부터 속성과 메서드들을 상속받습니다:
예시
#include <iostream>
using namespace std;
// 부모 클래스
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut! \n";
}
};
// 자식 클래스
class Car :public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand << " " << myCar.model;
return 0;
}

왜, 언제 상속을 사용해야 할까?
- 이것은 코드를 재사용할 때 유용합니다: 새로운 클래스를 만드는데, 기존 클래스의 속성과 메서드들 재사용할 때.
'C++ > 공부내용' 카테고리의 다른 글
C++ Classes - Inheritance / Multiple Inheritance (0) | 2022.06.29 |
---|---|
C++ Classes - Inheritance / Multilevel Inheritance (0) | 2022.06.29 |
C++ Classes - Encapsulation (0) | 2022.06.29 |
C++ Classes - Access Specifiers (0) | 2022.06.29 |
C++ Classes - Constructors (0) | 2022.06.29 |