Professional Documents
Culture Documents
Polymorphism
Polymorphism
Flexibility in approach:
By hiding data or abstracting details that are not needed for presentation, the programmer
achieves greater flexibility in approach.
Enhanced Security:
Abstraction gives access to data or details that are needed by users and hide the
implementation details, giving enhanced security to application.
Easier Replacement:
With the concept of abstraction in object-oriented programming language, it is possible to
replace code without recompilation. This makes the process easier and saves time for
users.
Modular Approach:
In object-oriented programming language C++, the abstraction concept helps users to
divide the project application into modules and test each of them separately. Then all
modules are integrated and ultimately tested together. This approach makes the
application development easier.
Practical example
Ex- For one operator operation ca take different from if operator is working on number
‘+’ than it gives sum of numbers & if this operation is working on string than it will give
concatenation string. This type of behavior is known as operator overloading.
Types of polymorphism
1. Runtime polymorphism
Resolving a function call at runtime is known as Runtime polymorphism.
It is also known as dynamic or late or runtime binding.
It is achieved through virtual function.
2. Compile time polymorphism
Resolving a function call at compile time is known as Runtime polymorphism.
It is known as static or early or compiles time binding.
It is achieved through function overloading and operator overloading.
Polymorphism
Function Overloading
Single function name can used to handle different number & different types of
arguments. Using single function name to perform different type of task is known as
function overloading.
Using Function overloading we can design a family of function with one function
name but with different argument lists.
A function call first matches the prototype having the same number and type of argument
and then calls the appropriate function for execution. A best match must be unique.
Example
//function prototyping
int volume(int);
double volume(double,int);
long volume(long,int,int);
main()
{
clrscr();
cout<<volume(10) <<"\n";
cout<<volume(2.5,8) <<"\n";
cout<<volume(100L,75,15) <<"\n";
getch();
}
//Function defination
int volume(int s) //cube
{
return (s*s*s);
}
double volume(double r,int h) //cylinder
{
return(3.14519*r*r*h);
}
long volume(long l,int b,int h) //rectangular
{
return (l*b*h);
}
Operator Overloading
Definition:-
C++ permits us to add two variables of user defined types with same syntax that is
applied to the basic types. This means that C++ has the ability to provide the operator
with special meaning for a data type.
private:
int data;
public:
increase()
{
data=0;
}
int display()
{
cout<<data<<endl;
}
void operator++()
{
data++;
}
};
void main()
{
clrscr();
increase i1,i2;
i1.display();
i2.display();
i1++;
i2++;
i1.display();
i2.display();
getch();
}
Friend Function with operator overloading
Friend function may be used in place of member functions for overloading a binary
operator, the difference being that a friend function requires two arguments to be
explicitly passed to it, while a member function requires only one.
Example
class myclass
{
int a;
int b;
public: