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.