LATE BINDING POLYMORPHISM in C++
Selecting functions during execution time is called late binding or dynamic binding or dynamic linkage. Late binding requires some overhead but provides increased power and flexibility. The late binding is implemented through virtual functions. An object of a class must be declared either as a pointer to a class or a reference to a class. For example, the following declaration of the virtual function shows how a late binding or run-time binding can be carried out:
class sample {
private:
int x;
fl oat y;
public:
virtual void display();
int sum();
};
class derivedD: public baseA
{
private:
int x;
fl oat y;
public:
void display(); //virtual
int sum();
};
int main()
{
baseA *ptr;
derivedD objd;
ptr = &objd;
-------
-------
ptr-> display(); //run time binding
ptr-> sum(); // compile time binding
return 0;
}
The keyword virtual must be followed by a return type of a member function if a run time is to be bound. Otherwise the compile time binding will be effected as usual. In the above program segment, only the display() function has been declared as virtual in the base class, whereas the sum() is non-virtual. Even though the message is given from the pointer of the base class to the objects of the derived class, it will not access the sum() function of the derived class as it has been declared as non-virtual. The sum() function compiles only the static binding.
Example 1.
A program to demonstrate the run-time binding of the member functions of a class. The same message is given to access the derived class member functions from the array of pointers. As functions are declared as virtual, the C++ compiler invokes the dynamic binding.
//virtual function
//demonstration of run time binding using
//array of pointers
#include <iostream>
using namespace std;
class baseA {
public:
virtual void display() {
cout << “ One \n”;
}
};
class derivedB : public baseA
{
public:
virtual void display(){
cout << “ Two \n”;
}
};
class derivedC : public derivedB
{
public:
virtual void display(){
cout << “ Three \n”;
}
};
int main()
{
//defi ne three objects
baseA obja;
derivedB objb;
derivedC objc;
baseA *ptr[3]; //defi ne an array of pointers to baseA
ptr[0] = &obja;
ptr[1] = &objb;
ptr[2] = &objc;
for (int i = 0; i<=2; i++)
ptr[i]->display(); //same message for all objects
return 0;
}
Output of the above program
One
Two
Three
Example 2.
A program to illustrate the static binding of the member functions of a class.
//accessing member functions with pointers
#include <iostream>
using namespace std;
class base {
private:
int x;
fl oat y;
public:
void getdata();
void display();
};
class derivedB : public base {
private :
int rollno;
char name[20];
public :
void getdata();
void display();
};
void base :: getdata()
{
cout << “ enter an integer \n”;
cin >> x;
cout << “ enter a real number \n”;
cin >> y;
}
void base :: display()
{
cout << “ entered numbers are x = ” << x << “ and y = ” << y;
cout << endl;
}
void derivedB :: getdata()
{
cout << “ enter roll number of a student ? \n”;
cin >> rollno;
cout << “ enter name of a student ? \n”;
cin >> name;
}
void derivedB:: display()
{
cout << “ Roll number student’s name \n”;
cout << rollno << ‘\t’ << name << endl;
}
int main()
{
base *ptr;
derivedB obj;
ptr = &obj;
ptr->getdata();
ptr->display();
return 0;
}
Output of the above program
enter an integer
10
enter a real number
2.2
entered numbers are x = 10 and y = 2.2
Example 3.
A program to illustrate the dynamic binding of member functions of a class.
//accessing member functions with pointers
#include <iostream>
using namespace std;
class base {
private:
int x;
fl oat y;
public:
virtual void getdata();
virtual void display();
};
class derivedB : public base {
private:
long int rollno;
char name[20];
public:
void getdata();
void display();
};
void base :: getdata()
{
cout << “ enter an integer \n”;
cin >> x;
cout << “ enter a real number \n”;
cin >> y;
}
void base :: display()
{
cout << “ entered numbers are x = ” << x << “ and y = ” << y;
cout << endl;
}
void derivedB :: getdata()
{
cout << “ enter roll number of a student ? \n”;
cin >> rollno;
cout << “ enter name of a student ? \n”;
cin >> name;
}
void derivedB :: display()
{
cout << “ Roll number student’s name \n”;
cout << rollno << ‘\t’ << name << endl;
}
int main()
{
base *ptr;
derivedB obj;
ptr = &obj;
ptr->getdata();
ptr->display();
return 0;
}
Output of the above program
enter roll number of a student?
20071
enter name of a student?
Suhail
Roll number student’s name
20071 Suhail
Example 4.
A program to demonstrate how to defi ne virtual functions with inline code substitution for run-time binding of the member functions of a class.
//accessing member functions with pointers
// virtual functions with inline code
#include <iostream>
using namespace std;
class base {
private:
int x;
fl oat y;
public:
virtual inline void getdata();
virtual inline void display();
};
class derivedB: public base {
private:
long int rollno;
char name[20];
public:
void getdata();
void display();
};
inline void base :: getdata()
{
cout << “ enter an integer \n”;
cin >> x;
cout << “ enter a real number \n”;
cin >> y;
}
inline void base :: display()
{
cout << “ entered numbers are x = ” << x << “ and y = ” << y;
cout << endl;
}
void derivedB :: getdata()
{
cout << “ enter roll number of a student ? \n”;
cin >> rollno;
cout << “ enter name of a student ? \n”;
cin >> name;
}
void derivedB :: display()
{
cout << “ Roll number student’s name \n”;
cout << rollno << ‘\t’ << name << endl;
}
int main()
{
base *ptr;
derivedB obj;
ptr = &obj;
ptr->getdata();
ptr->display();
return 0;
}
Output of the above program
enter roll number of a student?
20072
enter name of a student?
Ahmed
Roll number student’s name
20072 Ahmed
Example 5.
A program to access the members of the derived class objects through an array of pointers. In this program, only the base class member functions are preceded by the keyword virtual.
//accessing member functions with array of pointers
#include <iostream>
using namespace std;
class base {
private:
int x;
fl oat y;
public:
virtual void getdata();
virtual void display();
};
class derivedB: public base {
private:
long int rollno;
char name[20];
public:
void getdata();
void display();
};
class derivedC: public base {
private:
fl oat height;
fl oat weight;
public:
void getdata();
void display();
};
void base :: getdata()
{
cout << “ enter an integer \n”;
cin >> x;
cout << “ enter a real number \n”;
cin >> y;
}
void base :: display()
{
cout << “ entered numbers are x = ” << x << “ and y = ” << y;
cout << endl;
}
void derivedB :: getdata()
{
cout << “ enter roll number of a student ? \n”;
cin >> rollno;
cout << “ enter name of a student ? \n”;
cin >> name;
}
void derivedB :: display()
{
cout << “ Roll number student’s name \n”;
cout << rollno << ‘\t’ << name << endl;
}
void derivedC :: getdata()
{
cout << “ enter height of student ? \n”;
cin >> height;
cout << “ enter weight of student ? \n”;
cin >> weight;
}
void derivedC :: display()
{
cout << “ Height and weight of the student’s \n”;
cout << height << ‘\t’ << weight << endl;
}
int main()
{
base *ptr[3];
derivedB objb;
derivedC objc;
ptr[0] = &objb;
ptr[1] = &objc;
ptr[0]->getdata();
ptr[1]->getdata();
ptr[0]->display();
ptr[1]->display();
return 0;
}
Output of the above program
enter roll number of a student?
20073
enter name of a student?
Kandasamy
enter height of student?
173
enter weight of student?
78
Roll number student’s name
20073 Kandasamy
Height and weight of the student’s
173 78
Example 6.
A program to access the members of the derived class objects through an array of pointers. In this program, both the base class and the derived class member functions are preceded by the keyword virtual.
//accessing member functions with array of pointers
#include <iostream>
using namespace std;
class base {
private:
int x;
fl oat y;
public:
virtual void getdata();
virtual void display();
};
class derivedB: public base {
private:
long int rollno;
char name[20];
public:
virtual void getdata();
virtual void display();
};
class derivedC: public base {
private:
fl oat height;
fl oat weight;
public:
virtual void getdata();
virtual void display();
};
void base :: getdata()
{
cout << “ enter an integer \n”;
cin >> x;
cout << “ enter a real number \n”;
cin >> y;
}
void base :: display()
{
cout << “ entered numbers are x = ” << x << “ and y = ” << y;
cout << endl;
}
void derivedB :: getdata()
{
cout << “ enter roll number of a student ? \n”;
cin >> rollno;
cout << “ enter name of a student ? \n”;
cin >> name;
}
void derivedB :: display()
{
cout << “ Roll number student’s name \n”;
cout << rollno << ‘\t’ << name << endl;
}
void derivedC :: getdata()
{
cout << “ enter height of student ? \n”;
cin >> height;
cout << “ enter weight of student ? \n”;
cin >> weight;
}
void derivedC :: display()
{
cout << “ Height and weight of the student’s \n”;
cout << height << ‘\t’ << weight << endl;
}
int main()
{
base *ptr[3];
derivedB objb;
derivedC objc;
ptr[0] = &objb;
ptr[1] = &objc;
ptr[0]->getdata();
ptr[1]->getdata();
ptr[0]->display();
ptr[1]->display();
return 0;
}
Output of the above program
enter roll number of a student?
20074
enter name of a student?
Suhail
enter height of student?
167
enter weight of student?
87.5
Roll number student’s name
20074 Suhail
Height and weight of the student’s
167 87.5

0 comments
Post a Comment