-
-
Save burning-croissant/0b079c627b992d67cfe9f51417cb0550 to your computer and use it in GitHub Desktop.
Virtual Function Example in C++
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 Parent{ | |
public: | |
virtual void print(){ | |
cout<<"PARENT PRINT"<<endl; | |
} | |
void display(){ | |
cout<<"PARENT DISPLAY"<<endl; | |
} | |
}; | |
class Child:public Parent{ | |
public: | |
void print(){ | |
cout<<"CHILD PRINT"<<endl; | |
} | |
void display(){ | |
cout<<"CHILD DISPLAY"<<endl; | |
} | |
} | |
int main(){ | |
Parent *p; | |
Child c; | |
p = &c; | |
p->print(); // VIRTUAL | |
p->display(); // NON-VIRTUAL | |
// "CHILD PRINT" | |
// "PARENT DISPLAY" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment