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
I WILL GIVE BRAINLIEST
Lostsunrise [7]

Answer:

i know but you to help me ok this answer b

3 0
3 years ago
What software tool can you use to see the applications that are currently running?
Soloha48 [4]

Answer:

You can use the task manager to see what programs are currently running.

Explanation:

7 0
3 years ago
Read 2 more answers
Hi, I need help on a Circle Class Assignment that is due today. If anyone could please help me, I would greatly appreciate it. I
nikklg [1K]

Answer:

hmmm lets see

Explanation:

7 0
3 years ago
#Write a function called "scramble" that accepts a string #as an argument and returns a new string. The new string #should start
mojhsa [17]

Answer:

def scramble(s):

   if len(s) % 2 == 1:

       index = int(len(s)//2)

   else:

       index = int(len(s)/2)

   

   return s[index:] + s[:index]

Explanation:

Create a function called scramble that takes one parameter, s

Check the length of the s using len function. If the length is odd, set the index as the floor of the length/2. Otherwise, set the index as length/2. Use index value to slice the s as two halves

Return the second half of the s and the second half of the s using slice notation (s[index:] represents the characters from half of the s to the end, s[:index] represents the characters from begining to the half of the s)

8 0
4 years ago
1.
Anna11 [10]
1. True
2. False
3. True


Have a great day <3
3 0
3 years ago
Other questions:
  • A job application is a form used to make a job request.<br> True<br> False
    8·2 answers
  • A way to have cells in your spreadsheet change formats based on the value of the cells is called ________.
    8·1 answer
  • You are manually configuring a tcp/ip host. another administrator gives you the router's ip address. what is the tcp/ip term for
    6·2 answers
  • A document format is
    11·2 answers
  • What are the benefits of writing functions that use parameters and return? Try to list at least two.
    12·1 answer
  • What is the full meaning of Internet and WiFi
    6·2 answers
  • What are some best practices for file management
    8·1 answer
  • Your supervisor has asked you if it is possible to monitor servers for potential port scans via SNMP. What two OIDs can you prov
    13·1 answer
  • Write the following numbers in binary. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25
    11·1 answer
  • Film’s importance in today’s economy?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!