In: Computer Science
C++ Programming: Programming Design and Data Structures
Chapter 13 Ex 2
Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided.
Write a test program that tests various operations on the class rectangleType.
I need a main.cpp file
Given:
**************rectangleType.cpp********************
#include <iostream>
#include <cassert>
#include "rectangleType.h"
using namespace std;
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);
}
rectangleType::rectangleType(double l, double w)
{
setDimension(l, w);
}
rectangleType::rectangleType()
{
length = 0;
width = 0;
}
rectangleType rectangleType::operator++()
{
//increment the length and width
++length;
++width;
return *this; //return the incremented value of the object
}
rectangleType rectangleType::operator++(int u)
{
rectangleType temp = *this; //use this pointer to copy
//the value of the object
//increment the length and width
length++;
width++;
return temp; //return the old value of the object
}
rectangleType rectangleType::operator--()
{
//Decrement the length and width
assert(length != 0 && width != 0);
--length;
--width;
return *this; //return the incremented value of the object
}
rectangleType rectangleType::operator--(int u)
{
rectangleType temp = *this; //use this pointer to copy
//the value of the object
//Decrement the length and width
assert(length != 0 && width != 0);
length--;
width--;
return temp; //return the old value of the object
}
rectangleType rectangleType::operator+
(const rectangleType& rectangle) const
{
rectangleType tempRect;
tempRect.length = length + rectangle.length;
tempRect.width = width + rectangle.width;
return tempRect;
}
rectangleType rectangleType::operator-
(const rectangleType& rectangle) const
{
rectangleType tempRect;
assert(length >= rectangle.length &&
width >= rectangle.width);
tempRect.length = length - rectangle.length;
tempRect.width = width - rectangle.width;
return tempRect;
}
rectangleType rectangleType::operator*(const rectangleType&
rectangle) const
{
rectangleType tempRect;
tempRect.length = length * rectangle.length;
tempRect.width = width * rectangle.width;
return tempRect;
}
bool rectangleType::operator==
(const rectangleType& rectangle) const
{
return (area() == rectangle.area());
}
bool rectangleType::operator!=
(const rectangleType& rectangle) const
{
return (area() != rectangle.area());
}
bool rectangleType::operator<=
(const rectangleType& rectangle) const
{
return (area() <= rectangle.area());
}
bool rectangleType::operator<
(const rectangleType& rectangle) const
{
return (area() < rectangle.area());
}
bool rectangleType::operator>=
(const rectangleType& rectangle) const
{
return (area() >= rectangle.area());
}
bool rectangleType::operator>
(const rectangleType& rectangle) const
{
return (area() > rectangle.area());
}
ostream& operator<<(ostream& osObject,
const rectangleType& rectangle)
{
osObject << "Length = " << rectangle.length
<< "; Width = " << rectangle.width;
return osObject;
}
istream& operator>>(istream& isObject,
rectangleType& rectangle)
{
isObject >> rectangle.length >> rectangle.width;
return isObject;
}
******************rectangleType.h********************
#ifndef H_rectangleType
#define H_rectangleType
#include <iostream>
using namespace std;
class rectangleType
{
//Overload the stream insertion and extraction operators
friend ostream& operator<<(ostream&, const
rectangleType &);
friend istream& operator>>(istream&, rectangleType
&);
public:
void setDimension(double l, double w);
double getLength() const;
double getWidth() const;
double area() const;
double perimeter() const;
//Overload the arithmetic operators
rectangleType operator + (const rectangleType &) const;
rectangleType operator - (const rectangleType &) const;
rectangleType operator * (const rectangleType&) const;
//Overload the increment and decrement operators
rectangleType operator ++ (); //pre-increment
rectangleType operator ++ (int); //post-increment
rectangleType operator -- (); //pre-decrement
rectangleType operator -- (int); //post-decrement
//Overload the relational operators
bool operator == (const rectangleType&) const;
bool operator != (const rectangleType&) const;
bool operator <= (const rectangleType&) const;
bool operator < (const rectangleType&) const;
bool operator >= (const rectangleType&) const;
bool operator > (const rectangleType&) const;
//constructors
rectangleType();
rectangleType(double l, double w);
protected:
double length;
double width;
};
#endif
// rectangleType.h
#ifndef H_rectangleType
#define H_rectangleType
#include <iostream>
using namespace std;
class rectangleType
{
//Overload the stream insertion and extraction operators
friend ostream& operator<<(ostream&, const
rectangleType &);
friend istream& operator>>(istream&, rectangleType
&);
public:
void setDimension(double l, double w);
double getLength() const;
double getWidth() const;
double area() const;
double perimeter() const;
//Overload the arithmetic operators
friend rectangleType operator + (const rectangleType &, const
rectangleType &);
friend rectangleType operator - (const rectangleType&, const
rectangleType &);
friend rectangleType operator * (const rectangleType&, const
rectangleType&);
//Overload the increment and decrement operators
friend rectangleType operator ++ (rectangleType&);
//pre-increment
friend rectangleType operator ++ (rectangleType&, int);
//post-increment
friend rectangleType operator -- (rectangleType&);
//pre-decrement
friend rectangleType operator -- (rectangleType&, int);
//post-decrement
//Overload the relational operators
friend bool operator == (const rectangleType&, const
rectangleType&);
friend bool operator != (const rectangleType&, const
rectangleType&);
friend bool operator <= (const rectangleType&, const
rectangleType&);
friend bool operator < (const rectangleType&, const
rectangleType&);
friend bool operator >= (const rectangleType&, const
rectangleType&);
friend bool operator > (const rectangleType&, const
rectangleType&);
//constructors
rectangleType();
rectangleType(double l, double w);
protected:
double length;
double width;
};
#endif
//end of rectangleType.h
// rectangleType.cpp
#include <iostream>
#include <cassert>
#include "rectangleType.h"
using namespace std;
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);
}
rectangleType::rectangleType(double l, double w)
{
setDimension(l, w);
}
rectangleType::rectangleType()
{
length = 0;
width = 0;
}
rectangleType operator ++ (rectangleType &rect)
{
//increment the length and width
++rect.length;
++rect.width;
return rect; //return the incremented value of the
object
}
rectangleType operator ++ (rectangleType &rect, int)
{
rectangleType temp = rect; //use this pointer to copy
//the value of the object
//increment the length and width
rect.length++;
rect.width++;
return temp; //return the old value of the object
}
rectangleType operator -- (rectangleType &rect)
{
//Decrement the length and width
assert(rect.length != 0 && rect.width != 0);
--rect.length;
--rect.width;
return rect; //return the incremented value of the object
}
rectangleType operator -- (rectangleType &rect, int)
{
rectangleType temp = rect; //use this pointer to copy
//the value of the object
//Decrement the length and width
assert(rect.length != 0 && rect.width != 0);
rect.length--;
rect.width--;
return temp; //return the old value of the object
}
rectangleType operator + (const rectangleType &rect1, const
rectangleType &rect2)
{
rectangleType tempRect;
tempRect.length = rect1.length +
rect2.length;
tempRect.width = rect1.width + rect2.width;
return tempRect;
}
rectangleType operator - (const rectangleType &rect1, const
rectangleType &rect2)
{
rectangleType tempRect;
assert(rect1.length >= rect2.length && rect1.width >= rect2.width);
tempRect.length = rect1.length - rect2.length;
tempRect.width = rect1.width - rect2.width;
return tempRect;
}
rectangleType operator * (const rectangleType &rect1, const
rectangleType &rect2)
{
rectangleType tempRect;
tempRect.length = rect1.length *
rect2.length;
tempRect.width = rect1.width * rect2.width;
return tempRect;
}
bool operator == (const rectangleType &rect1, const
rectangleType &rect2)
{
return (rect1.area() == rect2.area());
}
bool operator != (const rectangleType &rect1, const
rectangleType &rect2)
{
return (rect1.area() != rect2.area());
}
bool operator <= (const rectangleType &rect1, const
rectangleType &rect2)
{
return (rect1.area() <= rect2.area());
}
bool operator < (const rectangleType &rect1, const
rectangleType &rect2)
{
return (rect1.area() < rect2.area());
}
bool operator >= (const rectangleType &rect1, const
rectangleType &rect2)
{
return (rect1.area() >= rect2.area());
}
bool operator > (const rectangleType &rect1, const
rectangleType &rect2)
{
return (rect1.area() > rect2.area());
}
ostream& operator<<(ostream& osObject,
const rectangleType& rectangle)
{
osObject << "Length = " << rectangle.length
<< "; Width = " << rectangle.width;
return osObject;
}
istream& operator>>(istream& isObject,
rectangleType& rectangle)
{
isObject >> rectangle.length >> rectangle.width;
return isObject;
}
//end of rectangleType.cpp
// main.cpp : C++ program to test various operations of Rectangle
#include "rectangleType.h"
#include <iostream>
using namespace std;
int main()
{
rectangleType r1(10,20), r2(5,15);
cout<<"R1 ==> Area :
"<<r1.area()<<" Perimeter :
"<<r1.perimeter()<<endl;
cout<<"R2 ==> Area :
"<<r2.area()<<" Perimeter :
"<<r2.perimeter()<<endl;
r1++;
cout<<"R1 ==> Length :
"<<r1.getLength()<<" Width :
"<<r1.getWidth()<<endl;
++r2;
cout<<"R2 ==> Length :
"<<r2.getLength()<<" Width :
"<<r2.getWidth()<<endl;
rectangleType rAdd = r1 + r2;
rectangleType rSub = r1 - r2;
rectangleType rMul = r1 * r2;
cout<<"RAdd ==> Length :
"<<rAdd.getLength()<<" Width :
"<<rAdd.getWidth()<<endl;
cout<<"RSub ==> Length :
"<<rSub.getLength()<<" Width :
"<<rSub.getWidth()<<endl;
cout<<"RMul ==> Length :
"<<rMul.getLength()<<" Width :
"<<rMul.getWidth()<<endl;
if(r1 > r2)
cout<<"R1 is greater than
R2";
else if(r1 < r2)
cout<<"R1 is less than
R2";
else
cout<<"R1 is equal than
R2";
return 0;
}
//end of main.cpp
Output: