Please help me on C++ using code blocks Define the class rectangleType with data
ID: 652405 • Letter: P
Question
Please help me on C++ using code blocks
Define the class rectangleType with data members: length and width of type double. Include the following public member functions:
? setDimension(double l, double w); getLength(); getWidth(); area(); perimeter(); print();
? setLength(double l); to set the length and a reference to the object (the address of the object) is returned.
? setWidth(double w); to set the width and returns the resulting object..
? operator+(); to add the dimensions of 2 rectangles and to return a reference to the resulting object.
? operator++(); to post-increment the dimensions of 2 rectangles and to return a reference to the resulting object.
? Operator==(); to return the result of equating 2 rectangles.
? Operator!=(); to return the negation of operator==().
? Operator>(); to return the result of comparing 2 rectangles for greater than relationship.
? Operator<(); to return the result of comparing 2 rectangles smaller than relationship.
? Constructor with the 2 default parameters set to 0 and have the same names as the data members (i.e. : length and width).
1.Study the testDriver and sample output results above and then explain the difference between Line#1 and Line#2.
2. Study the testDriver and sample output results, then explain why the last results for r1 and r4 are similar.
testProg.cpp
//Test Program: class rectangleType
#include
#include
#include "rectangleType.h"
using namespace std;
int main()
{
rectangleType r1, r2;
rectangleType r3(3, 6), r4(6, 4), r5, r6;
cout << fixed << showpoint << setprecision(2);
r1.setLength(15).setWidth(12.00); //Line #1
cout << "r1: ";
r1.print();
r2.setWidth(5.00).setLength(9.00); //Line #2
cout << "r2: ";
r2.print();
r5 = r1 + r2;
r6 = (r3++)++;
cout << " r5 (adding 2 rects (r1 & r2)): ";
r5.print();
cout << " r6 (post-incrementing r3) : ";
r6.print();
r1.setLength(3).setWidth(3);
r2.setLength(4).setWidth(4);
r3.setLength(5).setWidth(5);
r4 = r1 + r2 +r3; //Line#3
cout << " r1 (adding 3 rects): ";
r1.print();
cout << "r4 (adding 3 rects): ";
r4.print();
cout << "r2 (adding 3 rects): ";
r2.print();
cout << "r3 (adding 3 rects): ";
r3.print();
cout<<" Comparison of r2 && r3... ";
if (r2 == r3)
cout<<"r2 & r3 are equal";
else if (r2 > r3)
cout<<"r2 > r3";
else
cout<<"r2 < r3";
r2++;
cout<<" Comparison of r2 && r3 after r2++... ";
if (r2 == r3)
cout<<"r2 & r3 are equal";
else if (r2 > r3)
cout<<"r2 > r3";
else
cout<<"r2 < r3";
return 0;
}
Explanation / Answer
class Rectangle
{
public:
double length, width;
public:
void setDimensions(double a,double b)
{
length = a;
width = b;
}
void area()
{
double area = length*width;
}
void perimeter()
{
permeter = 2*(length + breadth);
}
void print()
{
cout<<"length:"<<length<<"width:"<<width<<"area:"<<area<<"Perimeter:"<<perimeter<<endl;
}
Rectangle void operator +(Rectangle r1,r2)// implementing operator overloading
{
Rectangle r3;
r3.length = r1.length + r2.length;
return r3;
}
Rectangle void operator ++(Rectangle r1)// implementing operator overloading
{
Rectangle r4;
r4.length = r1.length + 1;
r4.width = r1.width + 1;
return r4;
}
}
public void main()
{
Rectangle r1, r2, result1, result;
r1.getDimensions(5,7);
r2.getDimensions(7,9);
result1 = r1 + r2;
result1.area();
result1.perimeter();
result1.print();
result = r1++;
result.area();
result.perimeter();
result.print();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.