Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
com)
66
Polymorphism
Syllabus
Concepts of polymorphism, types of polymorphism, Overloading & overriding, Virtual function,
Static & dynamic binding.
Polymorphism
Q1.Define Polymorphism?
Ans. The word poly originated from a Greek word meaning many and morphism from
a Greek word meaning form, and thus polymorphism means many forms. In object
oriented programming, polymorphism refers to identically named methods (member
functions) that have different behavior depending on the type of object they refer.
Polymorphism is the process of defining a number of objects of different classes
into a group and call the methods to carry out the operation of the objects using
different function calls. Polymorphism means to carry out different processing steps by
functions having same messages - It treats objects of related classes in a generic
manner. The keyword virtual is used to perform the polymorphism concept in C++.
Polymorphism refers to the run time binding to a pointer to a method.
It is one of the important of object-oriented programming. It needs one name and
multiple Forms. The concept of polymorphism is implemented using function and
operator overloading. The overloaded member functions are selected for invoking the
function calls depending on the type and the number of arguments. When the function
name and Prototype is same in the base class and derived class which is not an function
overloading. But it is called a function overloading.
Polymorphism is the ability of an object or reference to take many different forms
at different instances. These are of two types one is the "compile time polymorphism"
and other one is the "run-time polymorphism".
Compile time polymorphism:
In this method object is bound to the function call at the compile time itself.
Run time polymorphism:
In this method object is bound to the function call only at the run time.
PolyMorphism
PolyMorphism
Compile Time
Compile Time
Polymorphism
Polymorphism
Function
Function
OverLoading
OverLoading
Operator
Operator
OverLoading
OverLoading
Rum Time
Rum Time
Polymorphism
Polymorphism
Virtual
Virtual
Function
Function
run-time
compile-time
Q4.What is Binding?
Ans.Binding refers to the act of associating an object or a class with its member. If we
can call a method fn() on an object obj of a class CC, we say that the object obj is binded
with the method fn(). This happens at compile time and is known as static or compile time binding. The calls to the virtual member functions are resolved during run-time.
This mechanism is known as dynamic binding. The most prominent reason why a
virtual function will be used is to have a different functionality in the derived class. The
difference between a non-virtual member function and a virtual member function is, the
non-virtual member functions are resolved at compile time.
Q5.What is Early Binding or compile time or static Binding polymorphism
Ans.Selecting a function in normal way, during compilation time is called as early
binding or static binding or static linkage. During compilation time, the C++ compiler
determines which function is used based on the parameters passed to the function or
the functions return type. The compiler then substitutes the correct function for each
invocation. Such compiler based substitutions are called static linkage.
By default, C++ follows early binding. With early binding, one can achieve greater
efficiency. Function calls are faster in this case because all the information necessary to
call the function are hard coded.
The purest object oriented programming language like Small talk permits only run
time binding of the methods, whereas C++ allows both compile time binding and run
time binding.
Q6.Write program to demonstrate compile time polymorphism
Ans. Compile time Polymorphism
#include <iostream>
#include <conio.h>
using namespace std;
int Add(int nX, int nY)
{
return nX + nY;
3
OverRiding
Define a new function with same name ( as in base class) in derived class is known as
method override. In the derived class base class defined method is hided.
Method overriding is redefining the method (member function) in the derived class with
the same name as that of public method of the base class.
hen a member function in the base class is overridden in the derived class, the object of the
derived class cannot access the functions original definition from the base. It only can
access the new definition implemented in the derived class.
The question is, if overriding is opposite to that of inheritance then why do we use
it ? The answer is simple. Inheritance can be used to reuse a class definition already
available. Suppose if we want to reuse the available class but a single member function
of the base is not satisfying your software requirements. That single function needs
update.
e solution is to directly modify that class and use it. Normally, every software component has the
owner and only owner is supposed to modify the code. Others cannot modify it. Another
reason for not directly modifying the available class is that class might be used by some
other system which requires maintaining the original function definition.
Therefore, the second solution is applied; create a new class definition from the
original by inheriting it. In the new derived -class insert a function definition for the
function which you want to modify. C++ gives you this flexibility of overriding functions
from the base just by redefining it with the same name in the derived class. Software
reusability gets widened due to overriding. Without which we would have reimplemented
the whole class again keeping all but one functions same.
Q9.State difference between function overloading and overriding
Ans.
Overriding
1.Different functions have same name
in different class.
11
The prototype of class of version of virtual function & all derived class
versions must be identical. I.e. its prototype should be same. If two function with
same name have different prototype C++ will be considered as overloading &
virtual keyword is ignored.
We cannot have virtual constructor but we can have virtual destructors are
possible.
While the base pointer can point to any point of derived object than the reverse is
not true.
When a base pointer points to a derived class, incrementing or decrementing will
not make point to the next object of derived class but instead it is related to its
base type.
If a virtual function is defined in the base class it is not necessary to redefined in
the derived class, in such case it will invoke the base class function.
Pure Virtual Function
Q17.Describe what is pure virtual function
Ans.Virtual function inside the main class must be declared to achieve polymorphism
and redefined it in the derived class. The function inside the base class is seldom(rarely)
used for performing any task. It only serves as a placeholder. Pure Virtual Function is a
Virtual function with no body.
16
};
void square:: getdata()
{
cout << enter the value of side x ? \n;
cin >>X;
}
void square:: disp()
{
cout<< value of x = y = << X << endl;
cout << Area of the square = << area();
cout << endl;
}
int square :: area()
{
int temp = X*X;
return(temp);
}
void rectangle :: getdata()
{
cout << enter the value of sides x and y ? \n;
Cin >> X >>y;
}
void rectangle:: display()
{
cout <<value of <<x<< and y=;
cout << y << endl;
cout << Area of the rectangle << area();
cout << endl;
}
int rectangle :: area()
{
int temp = X*y;
return(temp);
}
void main()
{
square sqobj;
rectangle rectobj;
square *ptr;
ptr = &sqobj;
ptr = &rectobj;
ptr->getdata() ;
ptr->area();
ptr->display();
}
21
25