-->

C++ Polymorphism 2




C++ Polymorphism

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. ... You have different classes with a function of the same name, and even the same parameters, but with different implementations.

Polymorphism in C++ - GeeksforGeeks

Polymorphism in C++ - Tutorialspoint

 

2nd example on  Polymorphism

if you want to download the .cpp file 

How to download

         Click Here

////Name Syed Syab Ahmad Shah
////Roll number ****
////University of SWAT
////BS SE
#include<iostream>
using namespace std;
class Shape
{
protected:
int width;
int height;
public:
int shape2(int a, int b)
{
height = a;
width = b;
}
};
class square: public Shape
{
public:
////square (int a, int b) : Shape (a,b) {}

int shape2()
{
cout<<"square class area: "<<endl;
return (width*width);
}
};
class rectangle: public Shape
{
public:
///rectangle (int a=0, int b=0) : Shape (a,b) {}

int shape2()
{
cout<<"rectangle class area: "<<endl;
return (width*height);
}
};
class triangle: public Shape
{
public:
////triangle (int a=0, int b=0) : Shape (a,b) {}

int shape2()
{
cout<<"triangle class area: "<<endl;
return ((width*height)/2);
}
};
int main()
{
triangle tri;

square squ;

rectangle rec;

Shape *shape1 = &tri;
Shape *shape3 = &squ;
Shape *shape4 = &rec;

shape1-> shape2(2,4);

shape3->shape2(5,7);

shape3->shape2(3,4);

tri.shape2();
squ.shape2();
rec.shape2();
}