Question

In: Computer Science

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 8
rectangle2.print(); //Line 9
cout << endl; //Line 10

rectangle3 = rectangle1 + rectangle2; //Line 11
cout << "Line 12: rectangle3: "; //Line 12
rectangle3.print(); //Line 13
cout << endl; //Line 14

rectangle4 = rectangle1 * rectangle2; //Line 15
cout << "Line 16: rectangle4: "; //Line 16
//print rectangl through << stream extraction operator; //Line 17
cout << rectangle4;
cout << endl; //Line 18

if (rectangle1 == rectangle2) //Line 19
cout << "Line 20: rectangle1 and "
<< "rectangle2 are equal." << endl; //Line 20
else //Line 21
cout << "Line 22: rectangle1 and "
<< "rectangle2 are not equal."
<< endl; //Line 22
return 0;
}

rectangleType.h

#include <iostream>
using namespace std;

class rectangleType {

//Overload the stream insertion and extraction operators

public:
void setDimension(double l, double w);
double getLength() const;
double getWidth() const;
double area() const;
double perimeter() const;
void print() const;
//Overload the operator +

//Overload the operator *

//Overload the operator ==

//Overload the operator !=
rectangleType();
rectangleType(double l, double w);
private:
double length;
double width;
};

rectangleType.cpp

#include "rectangleType.h"

void rectangleType::setDimension(double l, double w)
{
if (l >= 0)
length = l;
else
length = 0;
if (w >= 0)
width = w;
else
width = 0;
}
double rectangleType::getLength() const
{
return length;
}
double rectangleType::getWidth()const
{
return width;
}
double rectangleType::area() const
{
return length * width;
}
double rectangleType::perimeter() const
{
return 2 * (length + width);
}
void rectangleType::print() const
{
cout << "Length = " << length
<< "; Width = " << width;
}
rectangleType::rectangleType(double l, double w)
{
setDimension(l, w);
}

rectangleType::rectangleType()
{
length = 0;
width = 0;
}


rectangleType rectangleType::operator+ (const rectangleType& rectangle) const
{
//to-do:Task 1
}

rectangleType rectangleType::operator* (const rectangleType& rectangle) const
{
//to do: Task 2
}

bool rectangleType::operator== (const rectangleType& rectangle) const
{
//to-do: Task 3
}

bool rectangleType::operator!= (const rectangleType& rectangle) const
{
//to-do: Task 4
}

ostream& operator<< (ostream& osObject, const rectangleType& rectangle)
{
//to-do: Task 5
}

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

rectangleType.h

#include <iostream>
using namespace std;

class rectangleType
{

    //Overload the stream insertion and extraction operators

public:
    void setDimension(double l, double w);
    double getLength() const;
    double getWidth() const;
    double area() const;
    double perimeter() const;
    void print() const;
    //Overload the operator +
    rectangleType operator+(const rectangleType &rectangle) const;

    //Overload the operator *
    rectangleType operator*(const rectangleType &rectangle) const;

    //Overload the operator ==
    bool operator==(const rectangleType &rectangle) const;

    //Overload the operator !=
    bool operator!=(const rectangleType &rectangle) const;

    rectangleType();
    rectangleType(double l, double w);

private:
    double length;
    double width;
};

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

rectangleType.cpp

#include "rectangleType.h"

void rectangleType::setDimension(double l, double w)
{
    if (l >= 0)
        length = l;
    else
        length = 0;
    if (w >= 0)
        width = w;
    else
        width = 0;
}
double rectangleType::getLength() const
{
    return length;
}
double rectangleType::getWidth() const
{
    return width;
}
double rectangleType::area() const
{
    return length * width;
}
double rectangleType::perimeter() const
{
    return 2 * (length + width);
}
void rectangleType::print() const
{
    cout << "Length = " << length
         << "; Width = " << width;
}
rectangleType::rectangleType(double l, double w)
{
    setDimension(l, w);
}

rectangleType::rectangleType()
{
    length = 0;
    width = 0;
}

rectangleType rectangleType::operator+(const rectangleType &rectangle) const
{
    //to-do:Task 1
    rectangleType rt;
    rt.length = length + rectangle.length;
    rt.width = width + rectangle.width;
    return rt;
}

rectangleType rectangleType::operator*(const rectangleType &rectangle) const
{
    rectangleType rt;
    rt.length = length * rectangle.length;
    rt.width = width * rectangle.width;
    return rt;
    //to do: Task 2
}

bool rectangleType::operator==(const rectangleType &rectangle) const
{
    if (rectangle.length == length && rectangle.width == width)
        return true;
    return false;
    //to-do: Task 3
}

bool rectangleType::operator!=(const rectangleType &rectangle) const
{
    //to-do: Task 4

    if (!(rectangle.length == length && rectangle.width == width))
        return true;
    return false;
}

ostream &operator<<(ostream &osObject, const rectangleType &rectangle)
{
    osObject<<"Length: "<<rectangle.getLength()<<"\nWidth: "<<rectangle.getWidth()<<endl;
    return osObject;
    //to-do: Task 5
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

demo.cpp

#include <iostream>
#include "rectangleType.cpp"
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 8
        rectangle2.print();                             //Line 9
        cout << endl;                                     //Line 10

        rectangle3 = rectangle1 + rectangle2; //Line 11
        cout << "Line 12: rectangle3: ";    //Line 12
        rectangle3.print();                                       //Line 13
        cout << endl;                                               //Line 14

        rectangle4 = rectangle1 * rectangle2; //Line 15
        cout << "Line 16: rectangle4: ";    //Line 16
        //print rectangl through << stream extraction operator; //Line 17
        cout << rectangle4;
        cout << endl; //Line 18

        if (rectangle1 == rectangle2) //Line 19
                cout << "Line 20: rectangle1 and "
                         << "rectangle2 are equal." << endl; //Line 20
        else                                                                             //Line 21
                cout << "Line 22: rectangle1 and "
                         << "rectangle2 are not equal."
                         << endl; //Line 22
        return 0;
}

Related Solutions

Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that...
Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that use these symbols but have a parameter list that includes a class ____. object address reference data type Flag this Question Question 210 pts When overloading the insertion operator to process a Complex object, it’s important to understand that you’re overloading an operator in the ____ class. istream operator Complex ostream Flag this Question Question 310 pts ____ class variables are allocated memory locations...
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...
Wondering where to start on this C++ homework assignment involving friend functions and overloading operators implemented...
Wondering where to start on this C++ homework assignment involving friend functions and overloading operators implemented in a Rational class. These are the instructions: Your class will need to store two internal, integer values for each Rational number, the numerator (top) and denominator (bottom) of the fraction. It will have three constructor functions, with zero, one and two arguments, used as follows:     Rational test1, test2(10), test3(1, 2); The declaration for test1 calls the default (no argument) constructor, which should...
c++ using class... define operator overloading and give simple example how we can use operator overloading...
c++ using class... define operator overloading and give simple example how we can use operator overloading by writing simple program in which different operators are used to add, subtract, multiply and division.
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
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...
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.
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions...
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions below main. 1. Write a function harmonicMeans that repeatedly asks the user for two int values until at least one of them is 0. For each pair, the function should calculate and display the harmonic mean of the numbers. The harmonic mean of the numbers is the inverse of the average of the inverses. The harmonic mean of x and y can be calculated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT