Nrite the definition for a class called Rectangle that has int data members leng
ID: 3914133 • Letter: N
Question
Nrite the definition for a class called Rectangle that has int data members length and width. Define accessor and mutator functions. Define two constructors - i. default and ii. Constructor with two parameters. Define int perimeter0 to calculate and return the perimeter of the rectangle, int area0 to calculate and return the area of the rectangle. void show0 to display the output. Overload the equality operator , post increment operator++ as member functions to check if two rectangle objects (width and length) are same or not and to increase length and width Test your program by user given input for length and width.Explanation / Answer
#include <iostream>
using namespace std;
class Rectangle
{
private:
int width;
int length;
public:
Rectangle()// default constructor
{
width = 0;
length = 0;
}
Rectangle(int l,int w)// argument constructor
{
length = l;
width = w;
}
// get and set methods
void setLength(int length)
{
this->length = length;
}
int getLength()
{
return length;
}
void setWidth(int width)
{
this->width = width;
}
int getWidth()
{
return width;
}
int area()
{
return width*length;
}
int perimeter()
{
return 2*(length+width);
}
void show()
{
cout<<" Length of rectangle : "<<getLength();
cout<<" Width of rectangle : "<<getWidth();
cout<<" Area of rectangle : "<<area();
cout<<" Perimeter of rectangle : "<<perimeter();
}
bool operator==(Rectangle r1) // compare rectangles
{
if(this->length == r1.length && this->width == r1.width)
return true;
else
return false;
}
Rectangle& operator++(int)// post increment operator
{
this->length++;
this->width++;
return *this;
}
};
int main() {
Rectangle r(5,6);
r.show();
Rectangle r1(6,5);
if(r1 == r)
cout<<" Both rectangles are equal";
else
cout<<" Rectangles are not equal ";
r1++;
r1.show();
return 0;
}
Output:
Length of rectangle : 5
Width of rectangle : 6
Area of rectangle : 30
Perimeter of rectangle : 22
Rectangles are not equal
Length of rectangle : 7
Width of rectangle : 6
Area of rectangle : 42
Perimeter of rectangle : 26
DO ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.