Question
Program for - OPERATOR OVERLOADING the Rectangle.h and Rectangle.cc files provided with code to be written in main.cc * see implementations for four arithmetic operators,
Program for - OPERATOR OVERLOADING
the Rectangle.h and Rectangle.cc files provided with code to be written in main.cc
* see implementations for four arithmetic operators, three Boolean operators and the input and output operators. They have all been implemented as friends of the Rectangle class.
Requirements:
Implement all 9 operators without using friends. a. For the Boolean and arithmetic operators, you should do this by making the operators members of the Rectangle class. Make sure you use const to protect the object which is being used as the left-hand operand as well as the object that is passed in (Do not miss a const).
For the I/O operators you should do this by writing input and output functions in the class that do the job of the operator, and then have the operator call that member function. The prototypes for the input and output functions are included in the Rectangle class.
Combine the two constructors into a single constructor by using default arguments.
Write an application that allows the user to enter data for two Rectangles and a scalar (an integer), and then demonstrates all the operators using those objects.
*Rectangle.cc
#include "Rectangle.h"
#include
#include
#include
using namespace std;
// Constructors
Rectangle::Rectangle(){
length = 0;
width = 0;
area = 0;
}
Rectangle::Rectangle(double l, double w){
length = l;
width = w;
area = length * width;
}
// Member and friend functions
Rectangle operator + (const Rectangle& r1, const Rectangle& r2){
Rectangle tmp;
tmp.length = r1.length + r2.length;
tmp.width = r1.width + r2.width;
tmp.area = tmp.length * tmp.width;
return tmp;
}
Rectangle operator - (const Rectangle& r1, const Rectangle& r2){
Rectangle tmp;
if(r1.length >= r2.length){
tmp.length = r1.length - r2.length;
}
else{
tmp.length = 0;
}
if(r1.width >= r2.width){
tmp.width = r1.width - r2.width;
}
else{
tmp.width = 0;
}
tmp.area = tmp.length * tmp.width;
return tmp;
}
Rectangle operator / (const Rectangle& r1, int num){
Rectangle tmp;
tmp.length = r1.length / num;
tmp.width = r1.width / num;
tmp.area = tmp.length * tmp.width;
return tmp;
}
Rectangle operator * (const Rectangle& r1, int num){
Rectangle tmp;
tmp.length = r1.length * num;
tmp.width = r1.width * num;
tmp.area = tmp.length * tmp.width;
return tmp;
}
bool operator == (const Rectangle& r1, const Rectangle& r2){
return r1.length == r2.length && r1.width == r2.width;
}
bool operator < (const Rectangle& r1, const Rectangle& r2){
return r1.area < r2.area;
}
bool operator > (const Rectangle& r1, const Rectangle& r2){
return r1.area > r2.area;
}
void Rectangle::input(istream&ins){
/* Move the implementation code from the >> operator show below here.
Remember that since this function will be a member now, there will not be an r1.
The >> operator should now call this function.
Remove the friend keyword from the operator in the .h file and move the
function prototype to after the class declaration.
*/
}
void Rectangle::output(ostream& outs){
/* Repeat what you did for input except using the code for the << operator */
}
ostream& operator << (ostream& outs, const Rectangle& r1){
outs << "Length: " << r1.length << endl;
outs << " Width: " << r1.width << endl;
outs << " Area: " << r1.area << endl;
return outs;
}
istream& operator >> (istream& ins, Rectangle& r1){
cout << "Enter the length: ";
cin >> r1.length;
cout << " Enter the width: ";
cin >> r1.width;
r1.area = r1.length * r1.width;
return ins;
}
* RECTANGLE.h
#include
class Rectangle
{
public:
// CONVERT THESE CONSTRUCTORS INTO A SINGLE CONSTRUCTOR THAT USES
// DEFAULT ARGUMENTS
Rectangle();
Rectangle(double l, double w);
// Member functions
void input(std::istream& ins);
void output(std::ostream& outs);
double get_length()const {return length;}
double get_width()const {return width;}
double get_area()const {return area;}
// Friend functions
friend Rectangle operator + (const Rectangle& r1, const Rectangle& r2);
friend Rectangle operator - (const Rectangle& r1, const Rectangle& r2);
friend Rectangle operator * (const Rectangle& r1, int num);
friend Rectangle operator / (const Rectangle& r1, int num);
friend std::istream& operator >> (std::istream& fin, Rectangle& r);
friend std::ostream& operator << (std::ostream& fout, const Rectangle& r);
friend bool operator == (const Rectangle& r1, const Rectangle& r2);
friend bool operator < (const Rectangle& r1, const Rectangle& r2);
friend bool operator > (const Rectangle& r1, const Rectangle& r2);
private:
double length; // should not be able to go < 0
double width; // should not be able to go < 0
double area;
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started