Write a program to create a class CIRCLE with one
field as radius, used to calculate the area of a Circle. Create another class
RECTANGLE used to calculate the area of the rectangle which is a subclass
of CIRCLE class. Use the concept of single inheritance such that the radius
of circle class can be re-used as length in rectangle class. Take necessary
data members and member functions for both the classes to solve this
problem. All the data members are initialized through the constructors.
Show the result by accessing the area method of both the classes through the
objects of rectangle class.
#include <iostream>
#include <cmath>
using namespace std;
class Circle{
private:
float radius;
public:
Circle(float radius){
this->radius=radius;
}
void setRadius(float radius){
this->radius=radius;
}
float getRadius(){
return this->radius;
}
float calculateArea(){
return 3.14*this->radius*this->radius;
}
};
class Rectangle:public Circle{
private:
float width;
public:
Rectangle(float length,float width):Circle(length){
this->width=width;
}
void setWidth(float width){
this->width=width;
}
float getWidth(){
return this->width;
}
float calculateArea(){
return this->getRadius()*this->width;
}
};
int main()
{
float radius=5;
Circle circle(radius);
cout<<"Circle Radius: "<<circle.getRadius()<<"\n";
cout<<"Circle Area: "<<circle.calculateArea()<<"\n\n";
Rectangle rectangle(4,5);
cout<<"Rectangle Length: "<<rectangle.getRadius()<<"\n";
cout<<"Rectangle Width: "<<rectangle.getWidth()<<"\n";
cout<<"Rectangle Area: "<<rectangle.calculateArea()<<"\n";
cin>>radius;
return 0;
}
Comments
Leave a comment