2. 1. It’s a greek word.
means
forms
means
many
In programming polymorphism is the ability for objects of
different classes related by inheritance to response
differently to same function call.
3. A FATHER TO
HIS CHILD
A BROTHER
TO HIS
SISTER
A TEACHER TO
HIS STUDENTS
A SON TO HIS
PARENTS
4. When a program send the shape parameter for a triangle it will work as
When a program send the shape parameter for a rectangle it will work as
Base
Height
Wide
Length
5. After the inheritance it is another important feature
of OOP.
Polymorphism is use to keep the interface to base
class to its deived classes.
Polymorphism can achieved by mean of virtual
functions.
In Polymorphism one pointer to a base class object
may also point any object of its derived class.
7. #include <iostream>
using namespace std;
class Shape {
protected:
int width, 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); }
};
class Triangle: public Shape {
public: Triangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2); }
};
Int main(){
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
shape = &rec;
shape->area();
shape = &tri;
shape->area();
return 0;
}