Skip to content

Instantly share code, notes, and snippets.

@always-hii
Created May 15, 2018 11:57
Show Gist options
  • Save always-hii/0b079c627b992d67cfe9f51417cb0550 to your computer and use it in GitHub Desktop.
Save always-hii/0b079c627b992d67cfe9f51417cb0550 to your computer and use it in GitHub Desktop.
Virtual Function Example in C++
#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