Last active
August 29, 2015 14:03
-
-
Save honux77/dc610bb25288fde7f501 to your computer and use it in GitHub Desktop.
c++ inheritance example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| class Base | |
| { | |
| public: | |
| void foo1() { | |
| cout << "BASE foo1" << endl; | |
| } | |
| virtual void foo2() { | |
| cout << "Base foo2" << endl; | |
| } | |
| }; | |
| class Derived : public Base | |
| { | |
| public: | |
| void foo1() { | |
| cout << "Derived foo1" << endl; | |
| } | |
| void foo2(){ | |
| cout << "Derived foo2" << endl; | |
| } | |
| }; | |
| /** | |
| * result of main is | |
| * BASE foo1 | |
| * Derived foo2 | |
| * Derived foo2 | |
| * Derived foo2 | |
| **/ | |
| int main() | |
| { | |
| Base *bar = new Derived(); | |
| bar->foo1(); | |
| bar->foo2(); | |
| Derived *bar2 = new Derived(); | |
| bar2->foo1(); | |
| bar2->foo2(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment