Answer to Question #247966 in C++ for Avi

Question #247966

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.


1
Expert's answer
2021-10-07T03:57:36-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS