OOP-L04-R-Inheritance using Shape

#include<iostream>
using namespace std;
#define PI 3.1416
class Shape{ // Parent/Super/Base class
int x; // private member will not inherited
protected: // protected, if private member needs to inherit
float area; // Area of a shape
float peri; // Perimeter of a shape
public:
void show_data(){
cout<<“Area: “<<area<<endl;
cout<<“Peri: “<<peri<<endl;
}
};
// inheritance mode:
// public: public->public; protected -> protected
// protected: public, protected >> protected
// private: public, protected >> private
class Circle: public Shape{ //Single level // child/sub/derived class
float r;
public:
void get_data(){
cout<<“Enter Radius: “;
cin>>r;
}
void calc_area(){
get_data(); // self
area = PI * r * r; // parent class member
peri = 2 * PI * r; // parent class member
show_data(); // parent class member
}
};
class Rectangle: public Shape{
int width, height;
public:
void get_data(){
cout<<“Enter Width and height: “;
cin>> width >> height ;
}
void calc_area(){
get_data(); // self
area = width * height; // parent class member
peri = 2 * (width + height); // parent class member
show_data(); // parent class member
}
};
int main(){
Circle c1;
c1.calc_area();

Rectangle r1;
r1.calc_area();

return 0;
}

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>