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
IgorLugansk [536]
4 years ago
15

This chapter uses the class rectangleType to illustate how to overload the operators +, *, ==, !=, >>, and <<. In th

is exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts 1 to 3.
1. Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they must be postive.)

2. Overload the binary operator - to subtract the dimensions of one rectangle from the corresponding dimensions of another rectangle. If the resulting dimensions are not positive, output an appropriate message and do not perform the operation.

3. The operators == and != are overloaded by considering the lengths and widths of rectangles. Redefine the functions to overload the relational operator by considering the areas of rectangles as follows: Two rectangles are the same, if they have the same area; otherwise, the rectangles are not the same. Similary, rectangle yard1 is greater than rectangle yard2 if the area of yard1 is greater than the area of yard2. Overload the remaining relational operators using similar definitions.

4. Write the definitions of the functions to overload the operators defined in parts a to c.

5. Write a test program that tests various operations on the class rectangleType.

class rectangleType {

Computers and Technology
1 answer:
sattari [20]4 years ago
4 0

Answer:

Check the attached image for code screenshot and output.

Explanation:

#######################################

      Rectangle.cpp

#######################################

#include "Rectangle.h"

Rectangle::Rectangle() {

       length = 0;

       width = 0;

}

Rectangle::Rectangle(double newLength, double newWidth) {

       length = newLength;

       width = newWidth;

}

void Rectangle::setLength(double l) {

       length = l;

}

void Rectangle::setWidth(double w) {

       width = w;

}

double Rectangle::getLength() {

       return length;

}

double Rectangle::getWidth() {

       return width;

}

double Rectangle::computeArea() {

       return length * width;

}

double Rectangle::computePerimeter() {

       return 2 * (length + width);

}

Rectangle Rectangle::operator++ () {

       length++;

       width++;

       return *this;

}

Rectangle Rectangle::operator++ (int) {

       Rectangle r(length, width);

       ++length;

       ++width;

       return r;

}

Rectangle Rectangle::operator-- () {

       if(length > 0) {

               length--;

       }

       if(width > 0) {

               width--;

       }

       return *this;

}

Rectangle Rectangle::operator-- (int) {

       Rectangle r(length, width);

       if(length > 0) {

               length--;

       }

       if(width > 0) {

               width--;

       }

       return r;

}

Rectangle Rectangle::operator- (Rectangle other) {

       

       if(length > other.length && width > other.width) {

               length--;

               width--;

       } else {

               cout << "invalid operation. Subtrated Rectangle is bigger" << endl;

       }

       return *this;

}

bool Rectangle::operator==(Rectangle other) {

       return (length == other.length) && (width == other.width);

}

bool Rectangle::operator!=(Rectangle other) {

       return (length != other.length) || (width != other.width);

}

void Rectangle::printDetails() {

       cout << "Rectangle Report" << endl;

       cout << "Dimensions: " << length << " X " << width << endl;

       cout << "Area: " << computeArea() << endl;

       cout << "Perimeter: " << computePerimeter() << endl;

       cout << "********************" << endl;

}

#######################################

        Rectangle.h

#######################################

#include<iostream>

#include<iomanip>

using namespace std;

class Rectangle {

       double length, width;

       public:

       Rectangle();

       Rectangle(double newLength, double newWidth);

       void setLength(double l);

       void setWidth(double w);

       double getLength();

       double getWidth();

       double computeArea();

       double computePerimeter();

       

       Rectangle operator++ ();

       Rectangle operator++ (int);

       

       Rectangle operator-- ();

       Rectangle operator-- (int);

       Rectangle operator- (Rectangle r);

       bool operator== (Rectangle r);

       bool operator!= (Rectangle r);

       

       void printDetails();

};

#######################################

           main.cpp

#######################################

#include "Rectangle.h"

       // Ask the user to type in a length and width and

       // create an object called rect2 of the rectangle class

       // See output for format

       cout << "Enter the length of rectangle 3: ";

       cin >> l;

       cout << "Enter the width of rectangle 3: ";

       cin >> w;

       Rectangle rect3(l, w);

       cout << endl;

       cout.setf(ios::fixed);

       cout.precision(1);

       // Using the member function in the class, print rect1, rect2,

       // and rect3 details in that order

       rect1.printDetails();

       rect2.printDetails();

       rect3.printDetails();

       cout << endl;

       // Print each rectangle in the format shown on the output

       cout << "Rectangle 1: " << rect1.getLength() << " X " << rect1.getWidth() << endl;

       cout << "Area: " << rect1.computeArea() << " Perimeter: " << rect1.computePerimeter() << endl;

       cout << "Rectangle 2: " << rect2.getLength() << " X " << rect2.getWidth() << endl;

       cout << "Area: " << rect2.computeArea() << " Perimeter: " << rect2.computePerimeter() << endl;

       cout << "Rectangle 3: " << rect3.getLength() << " X " << rect3.getWidth() << endl;

       cout << "Area: " << rect3.computeArea() << " Perimeter: " << rect3.computePerimeter() << endl;

       if(rect2 == rect3) {

               cout << "rectangle 2 and 3 are same." << endl;

       } else {

               cout << "rectangle 2 and 3 are not same." << endl;

       }

       cout << "After incrementing rectangle 2: ";

       rect2++;

       rect2.printDetails();

   return 0;

}

You might be interested in
An array A[0..n - 2] contains `n-1` integers from 1 to `n` in increasing order. (Thus one integer in this range is missing.) Des
svetlana [45]

Answer:

I do not understand anything

8 0
3 years ago
Not all output display data for humans to read explain why​
Vadim26 [7]

Answer:

The eye has a very focused view that is optimised for perceiving movement.Human cannot see clearly outside an -5 degree cone of foveal vision.

8 0
3 years ago
When applying a filter to a text layer, how can you ensure that the types editability is not lost
yarga [219]

Answer:

Convert the layer into a smart object.

Explanation:

7 0
4 years ago
Are Most job applications are online
Ahat [919]

Answer:

i would say yes since technology has evolved a lot over the years

Explanation:

5 0
3 years ago
Look at the following HTML form. Which description is correct? Please enter your discount code:
jasenka [17]

Answer:

<form action = "discount.php" method = "post" > discount method can be extract from POST method

Explanation:

<form action = "discount.php" method = "post" >

<input type = "text" size = "12" name = "code" >

<input type = "submit" value = "Process" >

</form>

6 0
3 years ago
Other questions:
  • What term refers to a piece of software that interfaces with the hardware on your computer?
    10·2 answers
  • Which of the following is the definition of Internet Protocol Security ( IPSec)? A remote access client/server protocol. It is a
    13·2 answers
  • What is C.R.A.T systems used for?
    9·1 answer
  • To save time and avoid formatting errors, you can use the__ to apply custom formatting to other places in your presentation and
    13·1 answer
  • What is your favorite food
    11·2 answers
  • Which of the following scan only works if operating system’s TCP/IP implementation is based on RFC 793?
    12·1 answer
  • Identify the problems that computer program bugs can cause. Check all that apply.
    9·2 answers
  • Please can someone help my assignment.​
    13·1 answer
  • In what ways can information be slanted in a news report? List at least five ways.
    5·2 answers
  • Out of all the social media tools available today, the best tool to start when beginning your social marketing efforts is which
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!