example1_basic_use.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

class Base {
public:
    virtual void show() {
        std::cout << "Base class show() function called." << std::endl;
    }
};

class Derived : public Base {
public:
    void show() override {  // Override the base class function
        std::cout << "Derived class show() function called." << std::endl;
    }
};

int main() {
    Base* basePtr;
    Derived derivedObj;

    basePtr = &derivedObj;

    // This will call the derived class's show() method
    basePtr->show();

    return 0;
}
Back to virtual_function