Answer to Question #172749 in C++ for Panjumin

Question #172749

Define a class Shape and include length,width attribute as protected and include member functions setwidth(int w) and setHeight(int h).Derive a class Rectangle and include calculate_Area() and display area of rectangle. Create an object Rect to store value of length and breadth from user and derive objects Area .


1
Expert's answer
2021-03-22T01:59:11-0400
#include <iostream>


using namespace std;


class Shape{
	//length,width attribute as protected
protected:
	int length;
	int width;
public:
	//member functions setwidth(int w) and setHeight(int h).
	void setwidth(int w) {
		this->width=w;
	}
	void setHeight(int h) {
		this->length=h;
	}
};
//Derive a class Rectangle 
class Rectangle:public Shape{
public:
	//calculate_Area() and display area of rectangle. 
	int calculate_Area(){
		return this->length*this->width;
	}
};
//The start point of the program
int main (){
	 //Create an object Rect to store value of length and width from user and derive objects Area .
	Rectangle Rect;
	int length;
	int width;
	//get the length from the user
	cout<<"Enter the length: ";
	cin>>length;
	//get the width from the user
	cout<<"Enter the width: ";
	cin>>width;
	//set length
	Rect.setHeight(length);
	//set width
	Rect.setwidth(width);
	//display the area
	cout<<"\nThe area = "<<Rect.calculate_Area()<<"\n\n";
	
	//delay
	system("pause");
	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