#include <iostream>
using namespace std;
class base {
public:
void fun_1() { cout << "base-1\n"; }
virtual void fun_2() { cout << "base-2\n"; }
virtual void fun_3() { cout << "base-3\n"; }
virtual void fun_4() { cout << "base-4\n"; }
};
class derived : public base {
public:
void fun_1() { cout << "derived-1\n"; }
void fun_2() { cout << "derived-2\n"; }
void fun_4(int x) { cout << "derived-4\n"; }
};
int main()
{
base base_obj;
base* base_ptr;
derived derived_obj;
base_ptr = &derived_obj; // base *bp = new derived;
cout << "base_obj :\n";
base_obj.fun_1();
base_obj.fun_2();
base_obj.fun_3();
base_obj.fun_4();
cout << "derived_obj :\n";
derived_obj.fun_1();
derived_obj.fun_2();
derived_obj.fun_3();
derived_obj.fun_4(1);
cout << "derived_obj pointed by base_ptr :\n";
// Early binding because fun1() is non-virtual
// in base
base_ptr->fun_1();
// Late binding (RTP)
base_ptr->fun_2();
// Late binding (RTP)
base_ptr->fun_3(); // overriding되지 않음 : 기초 클래스와 동일함 함수명의 파생 클래스 함수 누락됨
// Late binding (RTP)
base_ptr->fun_4(); // overriding되지 않음 : 기초/파생 클래스의 함수명과 반환형이 같으나 인자의 개수가 달라 overriding 조건 누락됨
// Early binding but this function call is
// illegal(produces error) becasue pointer
// is of base type and function is of
// derived class
// base_ptr->fun_4(5);
}
computer science/OOP