Polymorphism

Polymorphism:

The word Poly means many and morphism means forms polymorphism is one thing but it has many features

Example

#include<iostream.h>

#include<conio.h>

class JAVAPOLYMORPHISMEXAMPLE1

{

protected: int wid1, hei1;

public: JAVAPOLYMORPHISMEXAMPLE(int a=0, int b=0)

{

Wid1=a;

Hei1=b;

}

int area()

{

cout<<“Parent class area\n”; return 0;

}

};

class RECTANGLE:public JAVAPOLYMORPHISMEXAMPLE1

{

public: RECTANGLE(int a=0, int b=0):JAVAPOLYMORPHISMEXAMPLE1(a, b)

{

}

int area()

{

cout<<“Rectangle class area\n”; return(wid1*hei1);

}

};

class TRIANGLE:public JAVAPOLYMORPHISMEXAMPLE1

{

public: TRIANGLE(int a=0, int b=0):JAVAPOLYMORPHISMEXAMPLE1(a, b)

{

}

int area()

{

cout<<“Triangle class area\n”; return(wid1*hei1/2);

}

};

void main()

{

clrscr();

JAVAPOLYMORPHISMEXAMPLE1 *sh1; RECTANGLE rec(10, 7);

TRIANGLE tri(10, 5);

Sh1 = &rec;

Sh1->area();

Sh1 = &tri;

Sh1->area();

getch();

}

Output
Parent class area

Parent class area

Leave a Reply

Your email address will not be published. Required fields are marked *