Question

In: Computer Science

This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==,...

This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts a to c.

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 positive.)

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. Similarly, 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.

Solutions

Expert Solution

#######################################
       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"

int main() {  
        // Declare any variables you need here
        double w, l;    

        // Create an object called rect1 of the rectangle class
        // Use the constructor with no formal parameters
        Rectangle rect1;

        // Create an object called rect2 of the rectangle class
        // Make the length be 5.2 and the width be 8.6
        Rectangle rect2(5.2, 8.6);

        // 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;
}



Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Related Solutions

1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >=...
1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >= as member operators. The comparison operators will compare students by last name first name and id number. Also, overload << and >> as friend operators in the Student class. Overload << and >> operators in Roster class as well to output Rosters in a nice format as well as input Roster. Provide a function sort in a Roster class as a private function to...
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
C++ Program Overload the following operators to work with the Rational class and add test cases...
C++ Program Overload the following operators to work with the Rational class and add test cases in the driver program. Make sure your driver program now tests each of these symbols for your Rational Class. + – * / == != < <= > >= << (stream insertion operator, use the toString function) >> (stream extraction operator) Make sure you test all 12 operators Example Run (Bold is input), everything else is a print statement using the overloaded operators Enter...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how to overload binary operators as friend functions. Show how to convert from a fundamental type to a user-defined type using a constructor. Understand exception handling mechanisms using try-catch block statements. Understand the Application Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how...
Lab Assignment Objectives 'Be able to overload combined binary operators as member operator functions. Show how to overload binary operators as friend functions. Show how to convert from a fundamental type to a user-defined type using a constructor. Understand exception handling mechanisms using try-catch block statements. Understand the Application Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the...
Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the widths for the 2 Rectangles and multiple the lengths of the two rectangles (similar to how the “+” operator added the widths and lengths of the 2 rectangles). Overload this operator using the non-member method. Please copy-paste code into MS Word document and then show some screenshots of you running and testing this method using your IDE. Using the program below, overload the Divide...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT