반응형
목차
[C++언어] 상속이란?
상속이란 부모 클래스(Base Class)와 자식 클래스(Derived Class) 두개의 클래스가 있을 때 자식 클래스에서 부모 클래스의 method 나 attribute를 물려 받아서 동일하게 가지는 것을 의미 합니다.
상속은 : (콜론)을 사용해서 전달 합니다.
- : (콜론)으로 상속 정의
- 정의된 클래스의 속성과 메소드를 가져옴
[C++언어] 상속 예제
코드>>
#include <iostream>
#include <string>
using namespace std;
class family {
public:
string address = "Seoul";
};
class son : public family {
public:
int olds = 16;
};
int main() {
son James;
cout << "James lives in " << James.address << " and he is " << James.olds << "\n";
return 0;
}
5~8번 라인 : 부모 클래스
10~13번 라인 : 자식 클래스
10번 라인 : 콜론을 통해서 family 클래스를 상속 받음, Public을 사용해서 외부에서 사용가능하게 선언
17번 라인 : son 클래스 값과 family 클래스 값을 출력
결과>>
James lives in Seoul and he is 16
반응형
'C언어 C++ Programming' 카테고리의 다른 글
[C++언어] 원하는 경로에 파일 읽고 쓰기 (Path, File Write/Read) (0) | 2022.01.06 |
---|---|
[C++언어]Class의 Protected 수정, 출력하여 사용하는 법 (예제 포함) (0) | 2022.01.04 |
[C++ 언어] 캡슐화란? Private 사용 방법 (Encapsulation) (0) | 2021.12.23 |
[C++언어]Method와 ::(쌍클론,범위 지정 연산자, Scope resolution operator) (0) | 2021.12.22 |
[C++언어] Public, Private 차이와 사용방법 (0) | 2021.12.21 |