Answered! Question: In this assignment, you are asked to write the definition for a rectangle class and test it with the test…

Question: In this assignment, you are asked to write the definition for a rectangle class and test it with the test program provided. The description of the rectangle class and the driver program are given on the attached file rectangle.cpp. Download the rectangle.cpp and without removing or changing any of the member functions or member variables of this class, complete 10 member functions bodies such that your program produces exact output as provided. You should keep the class definition and the main function in the same source file, and you are not allowed to use global variables. Your program should be well commented and in particular, begin with a series of comments specifying your name and lab number. (You should not change class definition or main function. Just complete the rest of the work.) rectangle.cpp below: #include using namespace std; class rectangle { public: rectangle(int len, int wid); //Parameterized Constructor rectangle(); //Default Constructor; Default lenght is 20 and default width is 10 void setWidth(int newWid); //Mutator function void setLength(int newLen);//Mutator function int getWidth() const;//Accessor function int getLength() const;//Accessor function void printRectangle(); //Print function int getArea();//Find Area; hint: Area is width * length int getPrimeter();//Find Perimeter; hint: Perimeter is (width + length)*2 bool equals(const rectangle& otherRectangle) const; //Comparison function private: int length; //variable to store length int width; //variable to store width }; //Start your class functions codes from here! // Driver program for the rectangle class int main() { rectangle s1; rectangle s2(5, 20); rectangle s3; //Display rectangles cout << “Rectangle s1 “; s1.printRectangle(); cout << “Rectangle s2 “; s2.printRectangle(); cout << “Rectangle s3 “; s3.printRectangle(); // Test Equality if (s1.equals(s2)) cout << “s1 and s2 are equal rectangle!” << endl; else cout << “s1 and s2 are unequal rectangle!” << endl; if (s1.equals(s3)) cout << “s1 and s3 are equal rectangle!” << endl; else cout << “s1 and s3 are unequal rectangle!” << endl; //Display area and perimeter cout << “Area of s1=” << s1.getArea() << endl; cout << “Area of s2=” << s2.getArea() << endl; cout << “Perimeter of s1=” << s1.getPrimeter() << endl; cout << “Perimeter of s2=” << s2.getPrimeter() << endl; return 0; }

Expert Answer

 Program code:

#include<iostream>
using namespace std;
class rectangle
{
public:
rectangle(int len, int wid);
//Parameterized Constructor
rectangle(); //Default Constructor; Default lenght is 20 and default width is 10
void setWidth(int newWid); //Mutator function
void setLength(int newLen);//Mutator function
int getWidth() const;//Accessor function
int getLength() const;//Accessor function
void printRectangle(); //Print function
int getArea();//Find Area; hint: Area is width * length
int getPrimeter();//Find Perimeter; hint: Perimeter is (width + length)*2
bool equals(const rectangle& otherRectangle) const; //Comparison function
private: int length; //variable to store length
int width; //variable to store width
};
rectangle::rectangle(int x,int y)
{
length=x;
width=y;
}
rectangle::rectangle()
{
length=20;
width=10;
}
void rectangle::setWidth(int x)
{
width=x;
}
void rectangle::setLength(int y)
{
length=y;
}
int rectangle::getWidth()const
{
return width;
}
int rectangle::getLength()const
{
return length;
}
void rectangle::printRectangle()
{
cout<<“Rectangle length= “<<length<<“tRectangle width= “<<width;
}
int rectangle::getArea()
{
return(width*length);
}
int rectangle::getPrimeter()
{
return(width+length)/2;
}
bool rectangle::equals(const rectangle& otherRectangle) const
{
if((this->length==otherRectangle.length)&&(this->width==otherRectangle.width))
return true;
else
return false;
}

// Driver program for the rectangle class
int main()
{
rectangle s1;
rectangle s2(5, 20);
rectangle s3;
//Display rectangles
cout << “Rectangle s1 “;
s1.printRectangle();
cout << “Rectangle s2 “;
s2.printRectangle();
cout << “Rectangle s3 “;
s3.printRectangle();
// Test Equality
if (s1.equals(s2))
cout << “ns1 and s2 are equal rectangle!” << endl;
else
cout << “ns1 and s2 are unequal rectangle!” << endl;
if (s1.equals(s3))
cout << “ns1 and s3 are equal rectangle!” << endl;
else
cout << “ns1 and s3 are unequal rectangle!” << endl;
//Display area and perimeter
cout << “Area of s1=” << s1.getArea() << endl;
cout << “Area of s2=” << s2.getArea() << endl;
cout << “Perimeter of s1=” << s1.getPrimeter() << endl;
cout << “Perimeter of s2=” << s2.getPrimeter() << endl;
system(“pause”);
return 0;
}

Still stressed from student homework?
Get quality assistance from academic writers!