Late Binding and Early Binding code example

Example 1: late binding and early binding in c++

#include<iostream>
using namespace std;
class Base {
   public:
   void display() {
      cout<<" In Base class" <<endl;
   }
};
class Derived: public Base {
   public:
   void display() {
      cout<<"In Derived class" << endl;
   }
};
int main(void) {
   Base *base_pointer = new Derived;
   base_pointer->display();
   return 0;
}

Example 2: late binding and early binding in c++

#include<iostream>
using namespace std;
class Base {
   public:
   virtual void display() {
      cout<<"In Base class" << endl;
   }
};
class Derived: public Base {
   public:
   void display() {
      cout<<"In Derived class" <<endl;
   }
};
int main() {
   Base *base_pointer = new Derived;
   base_pointer->display();
   return 0;
}

Tags:

Cpp Example