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 ํด๋ž˜์Šค์—์„œ ์ •์˜๋œ ๋ฉค๋ฒ„ํ•จ์ˆ˜๋“ค๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.