728x90
#include <iostream>
using namespace std;
class parents {
public:
//자식 클래스의 멤버 함수
void pFunc()
{
cout << "부모클래스" << endl;
}
//자식 클래스의 멤버 변수
int pnum;
};
class ch1: public parents {
public:
//자식 클래스의 멤버 함수
void ch1Func()
{
cout << "자식클래스" << endl;
}
//자식 클래스의 멤버 변수
int ch1num;
};
int main() {
//클래스를 인스턴스화 시킨다
//클래스 구조를 메모리에 그대로 만든다
//클래스명 > 객체명(변수명)
parents p1;
ch1 c1;
p1.pnum = 1;
p1.pFunc();
c1.ch1num = 2;
c1.ch1Func();
c1.pnum = 3;
cout << "부모 클래스의 변수 p1.pnum= " << p1.pnum << endl;
cout << "자식 클래스의 변수 c1.ch1num= " << c1.ch1num << endl;
cout << "자식 클래스에서 부모 변수 c1.pnum= " << c1.pnum << endl;
}
728x90
'언어는 과-학인가요? > C++' 카테고리의 다른 글
함수로 값바꾸기 (0) | 2020.11.14 |
---|---|
Protected, Private, Public 함수 : 이동시키기 (0) | 2020.10.31 |
생성자와 소멸자 (constructor and destructor) (0) | 2020.10.24 |
객체 지향 프로그래밍 (OOP) (0) | 2020.10.24 |
Namespace (이름공간)/ String (0) | 2020.10.10 |