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 .
#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;
}
Comments
Leave a comment