1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Fofino [41]
4 years ago
5

Write a program that will manipulate Rectangle objects for which you will create a Rectangle class.

Computers and Technology
1 answer:
vodomira [7]4 years ago
8 0

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<<"\nRectangle 1 : "<<std::endl;

std::cout<<one;

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

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

std::cout<<two;

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

Rectangle addAns = one+two;

Rectangle subAns = one - two;

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

std::cout<<addAns;

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

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

std::cout<<subAns;

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

std::cout<<std::endl;

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

std::cin>>ch;

}

}

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.

You might be interested in
Which creepy character is a user-generated myth in minecraft
horsena [70]
It would be slender man
7 0
3 years ago
Can. you please help me with this ​
slava [35]

Answer:

the answer is A

7 0
3 years ago
What do you understand by the term polysome?​
jasenka [17]

Answer:

A polyribosome (or polysome or ergasome) is a group of ribosomes bound to an mRNA molecule like “beads” on a “thread”. It consists of a complex of an mRNA molecule and two or more ribosomes that act to translate mRNA instructions into polypeptides.

6 0
3 years ago
What automatically created subdomain holds all the SRV records for Microsoft-hosted services, such as the global catalog, LDAP,
nevsk [136]

Answer:

The correct answer to the following question will be "_msdcs".

Explanation:

Msdcs directory known as Microsoft Domain Controller Catalog requires SRV documents that are used to identify domain controller for certain operations.

  • It automatically generated subdomain keeps all Microsoft-hosted resources SRV data, so it's the correct answer.
  • It includes common SRV documents, documents of LDAP, documents of Kpass, CNAME, etc.
7 0
4 years ago
Hodaya missed several days of her online nursing class and needs to catch up. The most effective way she can do
Snowcat [4.5K]

Answer

send an individual e-mail to her teacher for advice.

8 0
3 years ago
Read 2 more answers
Other questions:
  • Suppose you have a certain amount of money in a savings account that earns compound monthly interest, and you want to calculate
    10·2 answers
  • What is the circular motion that the earth makes in its orbit around the sun
    14·1 answer
  • when a page contains function is loaded the browser by passes the function without running it - true or false
    7·1 answer
  • Say you write a program that makes use of the Random class, but you fail to include an import statement for java.util.Random (or
    10·1 answer
  • Which of the following is an upper body workout
    6·2 answers
  • In the terms of OOP, in the microwave system, current time is a BLANK and change heat is a BLANK
    15·1 answer
  • Asymmetric key encryption combined with the information provided by a. certificate authority allows unique identification of the
    8·1 answer
  • Eric’s computer slows down when he open several computer programs. Why does this happen?
    15·2 answers
  • What are the steps to creating a blank database? Use the drop-down menus to complete them.
    9·1 answer
  • When you evaluate the use of digital media, how has it affected the newspaper business? The rise of digital media has caused new
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!