81.3k views
3 votes
Write a program that will manipulate Rectangle objects for which you will create a Rectangle class.

Declare a Rectangle class in a ".h" file with attributes and methods in the following points. Attributes should be private, methods should be generally public.

Define the Rectangle class in a ".cpp" file. Do not define anything inline in the ".h" file. Do not use 'namespace std' in either the .h or .cpp file of the class.

To use this class, write your main such that it will ask the user to input the two points and name for two rectangles. It will then calculate the result of the addition and subtraction operations. Then it will output the details of all 4 rectangles - to both screen and an output file. Allow the user to keep running the program in a loop, if desired.

A Rectangle is made up of two points (x.y) such that the first point (x1,y1) is the bottom left hand corner and second point (x2,y2) is the upper right hand corner. This will allow the Rectangle to be parallel to both axes.

Add a name attribute to the Rectangle which can be printed out from the program. Write two methods that return the area and perimeter of the rectangle respectively. Overload the input operator (>>) in the class to accept input for your class as needed. Do not use the cin operator in your class. Overload the output operator (<<) in the class such that it prints the following for a Rectangle object with name Rect1 (without the bullet points):

Rect1's four corners are at (5,3), (5,10), (8,10), (8,3).

Rect1's area is 21 and perimeter is 20.

Overload the addition operator (+) as a class member such that it returns the smalllest rectangle that will contain all four corners of both rectangles being added.

Overload the subtraction operator (-) as a friend function such that it returns a rectangle that is formed by the overlap of the two rectangles.

If there is no overlap between the two rectangles, it should return a rectangle where all four corners are at (0,0).

I do not understand the previous expert's comment of "dff". I am continuing to update this question in the hope that the expert will please inform me what information is needed so that a solution can be made.

1 Answer

5 votes

Answer:

REC.H ( save this file as "REC.H" )

#define REC_H

int min(int a, int b){

if(a<b) return a;

return b;

}

int max(int a, int b){

if(a>b) return a;

return b;

}

class Rectangle{

std::string name;

int x1,y1, x2, y2;

public:

//to overload cout and cin operator for rectangle object

friend std::ostream & operator << (std::ostream &out, const Rectangle &c);

friend std::istream & operator >> (std::istream &in, Rectangle &c);

int perimeter(){

int length = x2-x1;

int breadth = y1-y2;

return 2*(length+breadth);

}

int area(){

int length = x2-x1;

int breadth = y1-y2;

return length * breadth;

}

// operator overloading thorugh member function

Rectangle operator + (Rectangle const &two) {

Rectangle res;

res.name = "addRect";

res.x1 = min(this->x1, two.x1);

res.y1 = max(this->y1, two.y1);

res.x2 = max(this->x2, two.x2);

res.y2 = min(this->y2, two.y2);

return res;

}

// operator overloading thorugh friend function

friend Rectangle operator - (Rectangle const &one, Rectangle const &two);

};

std::ostream & operator << (std::ostream &out, const Rectangle &c)

{

out << c.name<<" "<< c.x1 << " " << c.y1 <<" "<< c.x2 <<" "<< c.y2 << std::endl ;

return out;

}

std::istream & operator >> (std::istream &in, Rectangle &c)

{

in >> c.name;

in >> c.x1;

in >> c.y1;

in >> c.x2;

in >> c.y2;

return in;

}

Rectangle operator-(Rectangle const &one, Rectangle const &two){

Rectangle res;

res.name = "subRect";

if(one.x1 > two.x2 || one.x2 < two.x1 || one.y1 < two.y2 || one.y2 > two.y1){

res.x1 = res.y1 = res.x2 = res.y2 = 0;

return res;

}

else{

if(one.x1 < two.x1){

res.x1 = two.x1;

res.y1 = min(two.y1, one.y1);

res.x2 = one.x2;

res.y2 = max(two.y2, one.y2);

}

else{

res.x1 = one.x1;

res.y1 = min(two.y1, one.y1);

res.x2 = two.x2;

res.y2 = max(two.y2, one.y2);

}

return res;

}

}

//header file code ends

// below is .cpp code

#include<iostream>

#include<string>

#include "REC.h" //including our REC.h file for accessing the declared class

int main(){

char ch='y';

while(ch=='y' || ch=='Y'){

Rectangle one, two;

std::cout<<"Enter Rectangle 1 (name, x1, y2, x2, y2): ";

std::cin>>one;

std::cout<<"Enter Rectangle 2 : ";

std::cin>>two;

std::cout<<"\\Rectangle 1 : "<<std::endl;

std::cout<<one;

std::cout<<"Perimeter = "<< one.perimeter()<<"\tArea = "<< one.area()<<"\\\\";

std::cout<<"Rectangle 2 : "<<std::endl;

std::cout<<two;

std::cout<<"Perimeter = "<< two.perimeter()<<"\tArea = "<< two.area()<<"\\\\";

Rectangle addAns = one+two;

Rectangle subAns = one - two;

std::cout<<"addAns : "<<std::endl;

std::cout<<addAns;

std::cout<<"Perimeter = "<< addAns.perimeter()<<"\tArea = "<< addAns.area()<<"\\\\";

std::cout<<"subAns : "<<std::endl;

std::cout<<subAns;

std::cout<<"Perimeter = "<< subAns.perimeter()<<"\tArea = "<< subAns.area()<<"\\";

std::cout<<std::endl;

std::cout<<"Want to run program again ? Enter 'y' or 'Y' else any key to exit : ";

std::cin>>ch;

}

}

Step-by-step explanation:

(1) To see how cpp code ends and how to indent code, please have a look on the attached file.

(2) Intuiton of formulae is also explained in deatiled way in the attached files.

Write a program that will manipulate Rectangle objects for which you will create a-example-1
Write a program that will manipulate Rectangle objects for which you will create a-example-2
Write a program that will manipulate Rectangle objects for which you will create a-example-3
User Ring
by
4.5k points