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
What data type of data type would you use to represent the registration letter of a car​
miss Akunina [59]

Answer:

If it is just one letter, use a character, but if it is more than one letter, use a string (string of characters)

6 0
3 years ago
Read 2 more answers
Give two examples of situations or applications where electrical circuits are used.
Pavlova-9 [17]

Answer:

lamps, electric motors

Explanation:

5 0
3 years ago
Word processing is the use of computer software to enter and edit text.
balu736 [363]

Answer:

True

Explanation:

Word processing software or word processing program handles functions related to entering and editing text in a text document. In essence a word processor processes text documents which includes processing entire papers, paragraphs, pages. With a word processing software text is entered  in the document mainly through typing in the letters with a keyboard and a word processor allows copying, deleting and formatting text through the use of built in commands

Examples of word processing programs are Microsoft Word, AppleWorks and OpenOffice.

7 0
3 years ago
What is RAM? explain it
Zina [86]
RAM is memory in the computer
3 0
3 years ago
Read 2 more answers
The undo can undo the last command only. Clicking on it more than once will do nothing.
Dmitriy789 [7]
True I hope I helped.
7 0
3 years ago
Read 2 more answers
Other questions:
  • 11. How much time will you save on a
    13·1 answer
  • Which feature of a blog marks a unique link to each post through which it can be recalled anytime?
    8·2 answers
  • Write a pseudocode algorithm which asks the user to enter a password.if the user enters “NotAtHome” then print “welcome”
    15·1 answer
  • One way to prepare for filing the PROFILE is to:
    12·2 answers
  • someone please tell me if you watch drag race (rupauls drag race) I need someone to talk to about it ​
    12·1 answer
  • Please help will give brainliest
    12·1 answer
  • Who is willam afton from five nights at freddy
    13·2 answers
  • I will give brainliest to the best answer. what is a good screen recorder
    11·1 answer
  • Which of the following makes Super Mario Run unique?
    6·1 answer
  • The agency that started ARPANET was looking for
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!