Wednesday, May 11, 2016

Polymorphism in C++ -TCCICOMPUTERCOACHING.COM



Polymorphism in C++ -TCCICOMPUTERCOACHING.COM

What is polymorphism?

Polymorphism means more than one form. "Poly" means "many" and "morph" means "form". 
It’s actually one of the important concept of object-oriented (OO) languages.
Which can improve the design, structure and reusability of code.
Example: function overloading, function overriding, virtual functions.
In programming languages, polymorphism means that same code or operators behaves differently in different places or block or functions.
For example, the + (plus) operator in C++:
Inta,b;
a+b;------------------------------ here integer addition
floata,b;
a+b;--------------------------------here float addition
Here, a+b seems same, both have addition functions, but behaviour and result for both addition are different.
Because one a+b is integer and another a+b is float.
So, this is called polymorphism. Means code a+b is same for both, yet implementation behaviour and result is different for both statements.
C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
Polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.
Imagine with inheritance that you have a rectangle and square, both inheriting from Shape. They both have same method area, same characteristics are side both have but with only inheritance they can differ because sides are the different for both shape, using polymorphism to change their kind of area.

Base class: Shape
Derived classes:Rectangle and Square
Behaviour: Area (method)
Kind of behavior: Different (polymorphism)
http://www.mathsisfun.com/geometry/images/area/rectangle.gif
The Area is the width times the height:
Area = w × h
http://www.mathsisfun.com/geometry/images/area/squar2.gifThe Area is the side length squared:
Area = a2 = a × a
#include <iostream>

Classshape {
public:
intwidth,height;

public:
Shape(int a=0, int b=0)
      {
width = a;
height = b;
      }
int area()
      {
cout<< "Parent class area :" <<endl;
return 0;
      }
};
class Rectangle: public Shape{
public:
Rectangle(int a=0, int b=0):Shape(a, b) { }
int area ()
      {
cout<< "Rectangle class area :" <<endl;
return (width * height);
      }
};
classsquare: public Shape{
public:
Square(int a=0):Shape(a, b) { }
int area ()
      {
cout<< "Square class area :" <<endl;
Return (a*a);
      }
};
// Main function for the program
int main( )
{
   Shape *shape;
   Rectangle r (10, 7);
Square s (10);

   // store the address of Rectangle
shape = &r;
   // call rectangle area.
shape->area();

   // store the address of Triangle
shape = &s;
   // call triangle area.
shape->area();

return 0;
}
When the above code is compiled and executed, it produces the following result:
Parent class area
Parent class area
If you like this post please like and share this post.
Visit us @ tccicomputercoaching.com
Call us @ 98256 18292




No comments:

Post a Comment