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
Explain why the scenario below fails to meet the definition an assembly line.
vichka [17]

Answer:

its not an assembly line

Explanation:

assembly lines have ppl who aren't experts and are taught to only do one simple part of a much much bigger thing

4 0
3 years ago
Contemporary pc operating systems and application software use a​ ________, which makes extensive use of​ icons, buttons,​ bars,
V125BC [204]

Contemporary pc operating systems and application software use a​ Graphical User Interface (GUI), which extensively uses​ icons, buttons,​ bars, and boxes to perform tasks.

What is a GUI?

A system of interactive visual components for computer software is known as a GUI (Graphical User Interface).

Compared to MS-DOS and the shell of Unix-like operating systems, a GUI is more user-friendly than a text-based command-line interface.

Objects like icons, cursors and buttons are included in a GUI. Sometimes these elements are enhanced with visual effects like transparency and sounds.

Learn more about Graphical User Interface here: brainly.com/question/14758410

#SPJ4

3 0
2 years ago
Having a low credit score can make it more difficult to: Obtain a car loan Open a new credit card Secure an apartment lease All
-Dominant- [34]

Answer:

all of the above

Explanation:

3 0
3 years ago
Write a function called printbackwards() that will work with a c++ string. the function will print any c++ string backwards. you
dangina [55]
Hi!

Well, this isn't exactly a question - but rather just a request. However, I'm going to attempt to try and <em>describe </em>to you how to approach this problem, instead of just writing the code for you and sending you on your way.

So, what's our general base goal here? We want to take a string into a function, and then print it out backwards. Seems simple enough!

Right away, we already have an idea how to set this code up. We need a main method which will call <em>PrintBackwards(), </em>which will have to take a parameter of type string. 

This would look something like <em>PrintBackwards(string baseString). </em>Inside this method, we'd have to do something so we can see each character in this string and then store it in a new string. 

I encourage you to try and tackle this on your own, but I can give you an idea. We can have a new valueless variable called reversedString, which will store our baseString but backwards.

We could try looping through the baseString for each character it possesses, and then keep adding onto our reversedString by doing something like +=. What I mean, is we'd access the very last index of baseString, and then keep appending characters into it. 

So our loop would look something like <em>for(int i = baseString.length; i > 0; i--) {}. 

</em>I haven't used C++ in awhile, so you'll have to find the specific syntax requirements. But with that loop, i represents the index of each character in baseString. It starts with the last index, and keeps going down in reverse.
<em>
</em>Inside our loop, we could do something like reverseString += baseString.index(i); Again, I don't remember the specific syntax - so you'll have to do this on your own.
<em>
</em>Hopefully, this helps! =)<em>
</em>
6 0
3 years ago
What information on social networking sites could be used to discriminate against a potential employee
NISA [10]

the answer on plato is b

political affiliations

3 0
3 years ago
Other questions:
  • 3.A customer has a system with a Gigabyte B450 Aorus Pro motherboard. He wants to upgrade the processor from the AMD Athlon X4 9
    11·1 answer
  • Open a command prompt on PC1. Issue the command to display the IPv6 settings. Based on the output, would you expect PC1 to be ab
    7·1 answer
  • Create a SavingsAccount class. Use a static data member annualInterestRate to store the annual interest rate for each of the sav
    10·1 answer
  • In addition to MLA, what are some other widely used style guides? Check all that apply.
    5·1 answer
  • Sometimes when a baby is born they need to be kept in an incubator for a period of time. A manufacturer would like to design a n
    11·1 answer
  • HELP ASAP!!<br> What is the difference between an internal event and an external event?
    10·1 answer
  • Help fast fast anyone
    10·1 answer
  • 6. What is Microsoft office word 2010 and who developed it?​
    15·1 answer
  • Drag the tiles to the correct boxes to complete the pairs.
    14·1 answer
  • What are some of the characteristics found in an editorial photograph?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!