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]
3 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]3 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
What unit is used for measure disk size ?
Inessa [10]
Inches are the unit of measurements on a disk
8 0
4 years ago
Why is soccer the most popular sport
riadik2000 [5.3K]

Soccer is the most 'popular' sport in the world because it is played more than any other by kids growing up. ... So, yeah, ya see…Soccer's not so much “The Beautiful Game'' as it is…

5 0
4 years ago
Read 2 more answers
Which type of books contains snapshots of steps that help to perform an activity?
stellarik [79]

Answer:

online book,,.........,.

5 0
3 years ago
Read 2 more answers
How are you doing this fine morning <br><br> hi
notka56 [123]
It's evening................∆
6 0
4 years ago
Read 2 more answers
Write a function (named n_pointed_star) to make the turtle draw an n-pointed star. The function should return nothing, and accep
Andreyy89

Answer:

import sys

import turtle

import random

def n_pointed_star(total_points):

if total_points <= 4:

 raise ValueError('Not enough total_points')

area = 150

for coprime in range(total_points//2, 1, -1):

 if greatest_common_divisor(total_points, coprime) == 1:

  start = turtle.position()

  for _ in range(total_points):

   turtle.forward(area)

   turtle.left(360.0 / total_points * coprime)

  turtle.setposition(start)

  return

def greatest_common_divisor(a, b):

while b != 0:

 a, b = b, a % b

return a

   

turtle.reset()

n_pointed_star(5)

Explanation:

  • Inside the n_pointed_star function, check whether the total no. of points are less than or equal to 4 and then throw an exception.
  • Loop through the total_points variable and check whether the result  from greatest_common_divisor is equal to 1 or not and then set the starting position of turtle and move it.
  • Create the greatest_common_divisor which takes two parameters a and b to find the GCD.
  • Finally reset the turtle and call the n_pointed_star function.
8 0
4 years ago
Other questions:
  • What is the Multiple Source Test
    15·2 answers
  • Which of the following statements regarding EFT is false? EFT still requires the endorsement of a check EFT allows payment to be
    11·1 answer
  • You asked your colleague to provide feedback on a blog post you recently wrote. When they sent you their feedback, they made edi
    10·1 answer
  • In a distributed database system, the data placement alternative with the highest reliability and availability is Group of answe
    9·1 answer
  • Given the business rule "an employee may have many degrees," discuss its effect on attributes, entities, and relationships. (Hin
    10·1 answer
  • What offers backup services that use cloud resources to protect applications and data from disruption caused by disaster? Multip
    9·1 answer
  • Using direct mapping, consider a 16-bit memory addresses, and a cache with 64 blocks, where each block is 8 bytes. What is the s
    8·1 answer
  • Write a method named removeRange that accepts an ArrayList of integers and two integer values min and max as parameters and remo
    10·1 answer
  • What makes manually cleaning data challenging?
    15·1 answer
  • What is the purpose of flight simulator programs, and what are some of the benefits of using them?
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!