카테고리 없음

Inheritance Overriding / Virtual Function

OKOK 2017. 10. 18. 14:19

#include <iostream>


using namespace std;



class A {

public:

void over() { cout << "A class over function call" << endl; }

};


class B : public A {

public:

void over() { cout << "B class over function call" << endl; }

};


int main() {

B b;

b.over();


system("pause");

return 0;


#include <iostream>


using namespace std;


class Parent {

public:

virtual void func() { cout << "Parent class func call" << endl; }

};


class Child :public Parent {

public:

virtual void func() { cout << "Child class func call" << endl; }

};


int main() {

Parent P, *pP;

Child C;


pP = &P;

pP->func();

pP = &C;

pP->func();


system("pause");

return 0;


순수 가상 함수는 반드시 자식 클래스에서 오버라이딩 되어야 합니다. 추상 클래스는 순수 가상 함수 선언을 하나라도 포함하면 그 클래스는 추상 클래스가 됩니다. 또한 추상 클래스를 상속하고, 그 상속받은 클래스에서 순수 가상 함수를 정의하지 않으면 그 클래스도 추상 클래스가 됩니다.


다중상속 

 #pragma warning(disable:4996)


#include <iostream>


using namespace std;


class ParentOne {

public:

void funcone() { cout << "ParenrOne class funcone() call" << endl; }

};


class ParentTwo {

public:

void functwo() { cout << "ParentTwo class functwo() call" << endl; }

};


class Child : public ParentOne, public ParentTwo {

public:

void func() { funcone(); functwo(); }

};


int main() {

Child child;


child.func();


system("pause");

return 0;

}