Static and Dynamic Binding

Estudies4you

3.2. Virtual Functions and Polymorphism
3.2.1. Static and Dynamic Binding

Q17. Explain in detail about the static and dynamic binding.
OR
Write about dynamic binding through virtual functions.
Answer :
Static Binding
Static binding or early binding is achieved through function overloading or operator overloading.
Members with same name possessing different arguments are known to the compiler during compilation time. They help the compiler to find the function definition with appropriate function call. This type of binding is also called compile time binding.

Example
class A //base class
{
inti;
public:
void display() //base class member function
{
--------
--------
}
};
class B: public A // derived class.
{
int j:
public:
void display() // member function of derived class
{
--------
--------
}
};
In this example, both base class and derived class have same member function display( ) However, the addresses for these functions are different. When these functions are invoked, the control jumps to various addresses of those member
functions. ‘

Example
#include<iostream.h>
#include<conio.h>
class Add
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Add obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}
Output
Explain the Types of Polymorphism in c++,polymorphism definition,define polymorphism,what is polymorphism in c++,types of polymorphism in c++,Compile time polymorphism in c++,Runtime polymorphism in c++,examples of Polymorphism,programs for Polymorphism,syntax for Polymorphism in c++,estudies4you,c++ notes jntuh,c++ lecture notes jntuh,c++ study material jntuh,oops using c++ lecture notes jntuh,

Dynamic Binding
Dynamic binding or late binding binds a function definition to an appropriate function call during run time. This type of binding is also called run time binding. Dynamic binding of member functions is achieved using virtual keyword.

Example
class A //base class
int i;
public:
virtual void display() //base class member function
{
--------
}
};
class B : public A //derived class
{
int j;
public:
virtual void display() //member function derived class
{
--------
}
};
In this example, both base class and derived class has same member function display() declared using “virtual” keyword.
The virtual function must be defined in the public section. The system recognizes this and performs dynamic binding. It detects all the references from the base class and assumes that the virtual functions in derived class match with the base class functions (parameter and parameter type). If they do not match then functions are assumed as overloaded functions.

(ii) Runtime Polymorphism
In this type of polymorphism, the most appropriate member function is called at runtime i.e., while the program is executing and the linking of function with a class occurs after compilation. Hence, it is called late binding or dynamic binding. It is implemented using virtual functions and the pointers to objects.

Example
#include<iostream.h>
#include<conio.h>
class Baseclass
{
public:
virtual void show()
{
cout<<“Base class \n”;
}
};
class Derivedclass: public Baseclass
{
public:
void show()
{
cout<<“\n Derived class \n”;
}
};
int main(void)
{
clrscr();
Baseclass *bp = new Derivedclass;
bp→show(); // RUN-TIME POLYMORPHISM
getch();
return 0;
}
Output
Explain the Types of Polymorphism in c++,polymorphism definition,define polymorphism,what is polymorphism in c++,types of polymorphism in c++,Compile time polymorphism in c++,Runtime polymorphism in c++,examples of Polymorphism,programs for Polymorphism,syntax for Polymorphism in c++,estudies4you,c++ notes jntuh,c++ lecture notes jntuh,c++ study material jntuh,oops using c++ lecture notes jntuh,

To Top