In: Computer Science
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
}
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;
}