Answer to Question #248114 in C++ for alee

Question #248114

(Single Inheritance) 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-07T14:00:04-0400
#include<iostream>
using namespace std;
class CIRCLE{
	private:
		int rad;
	public:
		CIRCLE(){
			
		}
		CIRCLE(int r){
			rad = r;
		}
		int getRad(){
			return rad;
		}
	 void area(){
	 cout<<"Area of the circle is: "<< 3.142 * rad * rad<<endl;
	 }
	 void setRad(int r){
	 	rad = r;
	 }
};


class RECTANGLE: CIRCLE{
	private:
		int width, length;
	public:
		RECTANGLE(int w, int le){
			width = w;
			setRad(le);
			length = getRad();
		}
		void area_cal(){
			cout<<"The area of the rectangle is:  "<<width * length<<endl;
		}
};


int main(){
	CIRCLE c;
	RECTANGLE r(10,7);
	
	r.area_cal();
}

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