Static Binding (์ ์ ๋ฐ์ธ๋ฉ)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Base{
public:
void f(){cout << "Base::f()" << endl;}
virtual void vf() {cout << "Base::vf()" << endl;}
};
class Derived:public Base{
public:
void f(){ cout << "Derived::f()" << endl;}
void vf() override { cout << "Derived::vf()" << endl;}
};
int main(){
Base base;
Derived derived;
base.f();
base.vf();
derived.f();
derived.vf();
return 0;
}
Base::f()
Base::vf()
Derived::f()
Derived::vf()
Program ended with exit code: 0
์ฐ๋ฆฌ๊ฐ ์ฌํ๊ป ๋ฐฐ์ ๋ ์์์์ย override
ย ,ย virtual
ย ์ ๋จ์ง ์์์ ๋ฐ์๋ค๋ ๊ฒ์ ๋ช
์ํ๋ ์ญํ ์ด๋ผ๊ณ ์๊ฐํ๋ค.
์ด๋ ๊ฒ ์ฐ๋ฆฌ๊ฐ ์์๋ ์์์ ๋จ์ํ๊ฒ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ย ์ ์ ๋ฐ์ธ๋ฉย ์ด๋ผ ํ๋ค.
์ฝ๋๋ฅผ ๋ณด๋ฉด, ๋ด๊ฐ ์์ฑํ ๊ฐ์ฒด์ ๋ํด ์ฎ์ฌ์๋ ํจ์๋ฅผ ์ ํด์ฃผ๋๋ฐ ์์ด ๋ด๊ฐ ์ ์ธํด์ค๋๋ก ๋์๋ค.
base ๊ฐ์ฒด์ ๋ํด base ํด๋์ค์์ ์ ์๋ ๋ฉค๋ฒํจ์๋ค๋ง ์ฌ์ฉ๊ฐ๋ฅํ๊ณ , derived ๊ฐ์ฒด์ ๋ํด์๋ Derived ํด๋์ค์์ ์ ์๋ ๋ฉค๋ฒํจ์๋ค๋ง ์ฌ์ฉ๊ฐ๋ฅํ๋ค.