Question

In: Computer Science

Write the definition for a generic class called Rectangle that has data members length and width....

  1. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions:
    setlength to set the length data member
    setwidth to set the width data member
    perimeter to calculate and return the perimeter of the rectangle
    area to calculate and return the area of the rectangle
    show to return a text to display the length and width of the rectangle
    sameArea that has one parameter of type Rectangle. sameArea returns 1 if the two Rectangles have the same area, and returns 0 if they don't.

Overload the following operators: -

Operators

Description

- (unary)

Length and width will change sign

+

Add bother areas and then find the ratio of the first Rectangle to find the new values of the length and the width

-

Subtract second area from first area and then find the ratio of the first Rectangle to find the new values of the length and the width

*

Multiply first area by second area and then find the ratio of the first Rectangle to find the new values of the length and the width

/

Divide first area by second area and then find the ratio of the first Rectangle to find the new values of the length and the width

<

Compare the areas

>

Compare the areas

<=

Compare the areas

>=

Compare the areas

==

Compare the areas

!=

Compare the areas

=

Assign the length and width to the first Rectangle

+=

Same as + operator and assign the result to the first Rectangle

-=

Same as - operator and assign the result to the first Rectangle

*=

Same as * operator and assign the result to the first Rectangle

/=

Same as / operator and assign the result to the first Rectangle

  1. Write the definitions for each of the above member functions.
  2. Write main function to create two rectangle pointers. Set the length and width of the first rectangle to 5 and 2. Set the length and width of the second rectangle to 5 and 18.9. Display each rectangle and its area and perimeter.
  3. Check whether the two Rectangles have the same area and print a message indicating the result. Set the length and width of the first rectangle to 15 and 6.3. Display each Rectangle and its area and perimeter again. Again, check whether the two Rectangles have the same area and print a message indicating the result.
  4. The language is visual studio c++.

Solutions

Expert Solution

#include<iostream>
using namespace std;
class Rectangle{
double length;
double width;
public:
double getlength()
{
return this->length;
}
double getwidth()
{
return this->width;
}
void setwidth(double width)
{
this->width=width;
}
void setlength(double length)
{
this->length=length;
}
double perimeter()
{
return 2*(length+width);
}
double area()
{
return length*width;
}
void show()
{
cout<<"The length of the rectangle: "<<length<<"\n";
cout<<"The width of the rectangle: "<<width<<"\n";
}
int sameArea(Rectangle R)
{
if(this->area()==R.area())
return 1;
else
return 0;
}
Rectangle operator -()
{
Rectangle res;
res.setlength(-this->length);
res.setwidth(-this->width);
return res;
}
Rectangle operator +(Rectangle R)
{
Rectangle res;
double newarea=this->area()+R.area();
double newlen=(newarea*this->length)/this->area();
res.setlength(newlen);
res.setwidth(newarea/newlen);
return res;
}
Rectangle operator -(Rectangle R)
{
Rectangle res;
double newarea=R.area()-this->area();
double newlen=(newarea*this->length)/this->area();
res.setlength(newlen);
res.setwidth(newarea/newlen);
return res;
}
Rectangle operator *(Rectangle R)
{
Rectangle res;
double newarea=this->area()*R.area();
double newlen=(newarea*this->length)/this->area();
res.setlength(newlen);
res.setwidth(newarea/newlen);
return res;
}
Rectangle operator /(Rectangle R)
{
Rectangle res;
double newarea=this->area()/R.area();
double newlen=(newarea*this->length)/this->area();
res.setlength(newlen);
res.setwidth(newarea/newlen);
return res;
}
bool operator <(Rectangle R)
{
if(this->area()<R.area())
return 1;
else
return 0;
}
bool operator >(Rectangle R)
{
if(this->area()>R.area())
return 1;
else
return 0;
}
bool operator >=(Rectangle R)
{
if(this->area()>=R.area())
return 1;
else
return 0;
}
bool operator <=(Rectangle R)
{
if(this->area()<=R.area())
return 1;
else
return 0;
}
bool operator ==(Rectangle R)
{
if(this->area()==R.area())
return 1;
else
return 0;
}
bool operator !=(Rectangle R)
{
if(this->area()!=R.area())
return 1;
else
return 0;
}
Rectangle operator =(Rectangle R)
{
this->setlength(R.length);
this->setwidth(R.width);
}
void operator +=(Rectangle R)
{
double newarea=this->area()+R.area();
double newlen=(newarea*this->length)/this->area();
this->setlength(newlen);
this->setwidth(newarea/newlen);
}
void operator -=(Rectangle R)
{
double newarea=R.area()-this->area();
double newlen=(newarea*this->length)/this->area();
this->setlength(newlen);
this->setwidth(newarea/newlen);
}
void operator *=(Rectangle R)
{
double newarea=this->area()*R.area();
double newlen=(newarea*this->length)/this->area();
this->setlength(newlen);
this->setwidth(newarea/newlen);
}
void operator /=(Rectangle R)
{
double newarea=this->area()/R.area();
double newlen=(newarea*this->length)/this->area();
this->setlength(newlen);
this->setwidth(newarea/newlen);
}
};
int main()
{
Rectangle R1,R2;
R1.setlength(5);
R1.setwidth(2);
R2.setlength(5);
R2.setwidth(18.9);
cout<<"The perimeter and area of first rectangle of length "<<R1.getlength()<<" and width "<<R1.getwidth()<<"is : "<<R1.perimeter()<<" and "<<R1.area()<<" respectively\n";
cout<<"The perimeter and area of second rectangle of length "<<R2.getlength()<<" and width "<<R2.getwidth()<<"is : "<<R2.perimeter()<<" and "<<R2.area()<<" respectively\n";
if(R1.sameArea(R2)==1)
cout<<"First Rectangle and Second Rectangle have same area\n";
else
cout<<"First Rectangle and Second Rectangle have different area\n";
R1.setlength(15);
R1.setwidth(6.3);
cout<<"The perimeter and area of first rectangle of length "<<R1.getlength()<<" and width "<<R1.getwidth()<<"is : "<<R1.perimeter()<<" and "<<R1.area()<<" respectively\n";
cout<<"The perimeter and area of second rectangle of length "<<R2.getlength()<<" and width "<<R2.getwidth()<<"is : "<<R2.perimeter()<<" and "<<R2.area()<<" respectively\n";
if(R1.sameArea(R2)==1)
cout<<"First Rectangle and Second Rectangle have same area\n";
else
cout<<"First Rectangle and Second Rectangle have different area\n";
return 0;
}


Related Solutions

Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
Write the definition for a generic / Template class called time that has hours and minutes...
Write the definition for a generic / Template class called time that has hours and minutes as structure. The class has the following member functions: SetTime to set the specified value in object ShowTime to display time object Sum to sum two time object & return time Write the definitions for each of the above member functions. Write main function to create three time objects. Set the value in two objects and call sum() to calculate sum and assign it...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions: void setlength(float) to set the length data member void setwidth(float) to set the width data member float perimeter() to calculate and return the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show() to display the length and width of the rectangle...
A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased
For the following exercises, use the written statements to construct a polynomial function that represents the required information.A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased by twice that amount, express the area of the rectangle as a function of x.  
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double area = 0;     area = width * length;     cout << "\n***maxArea called" << endl;          cout << "***       rectangleCount = " << Rectangle::rectangleCount << endl << endl;    } Compete this code to find the maximum area of rectangle between a,b,c
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT