Tuesday, May 25, 2021

simple program using pure virtual function in classes (OOP) (94)

 #include<iostream>

using namespace std;

class shape

{

protected:

string colour;

public:

shape()

{

  colour="";

}

virtual double calculateArea()=0;

virtual double calculateperimeter()=0;

};

class Rectangle:public shape

{

protected:

double width,height;

public:

Rectangle(double x,double y,string z)

{

width=x;

height=y;

colour=z;

}

double calculateArea()

{

return width*height;

}

double calculateperimeter()

{

return 2*height+width;

}

};

class circle:public shape

{

protected:

double radius;

public:

circle(double x,string y)

{

radius=x;

colour=y;

}

double calculateArea()

{

return 3.14*radius*radius;

}

double calculateperimeter()

{

return 2*3.14*radius;

}

};

class square :public Rectangle

{

protected:

int side;

public:

square(int j)

{

side=j;

}

};

int main()

{

shape *obj1;

rectangle obj2(5.6,7.8,"green");

obj1=&obj2;

obj1->calculateArea();

cout<<"the area of the rectangle is:"<< obj1->calculateArea()<<endl;

cout<<"the primeter of the rectangle is:"<<obj1->calculateperimeter();

circle obj3(8.9,"red");

obj1=&obj3;

cout<<"the area of the circle is:"<<obj1->calculateArea()<<endl;

cout<<"the primeter of the circle is:"<<obj1->calculateperimeter()<<endl;

square obj4;

obj4(90);

}

No comments:

Post a Comment