Skip to content

Instantly share code, notes, and snippets.

@dibakarsutradhar
Created September 8, 2018 19:49
Show Gist options
  • Save dibakarsutradhar/20b4b295ad365f2d5da594a6d7df1360 to your computer and use it in GitHub Desktop.
Save dibakarsutradhar/20b4b295ad365f2d5da594a6d7df1360 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
// Base Class
class Parent {
public:
void print(){
cout << "Parent Print Function" << endl;
}
};
// Derived Class
class Child : public Parent {
public:
// We're defining the same member function already exists in Parent
void print(){
cout << "Child Print Function" << endl;
}
};
// main function
int main() {
// Object of Parent Class
Parent obj1;
// Object of Child Class
Child obj2 = Child();
// obj1 will now call the print() function from Parent Class
obj1.print();
// obj2 now will override the print() function in Parent and call the print() function from Child
obj2.print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment