Question: Instructions Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been
Instructions 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.
main.cpp:
#include
#include "rectangleType.h"
int main()
{
rectangleType r1(5, 10);
rectangleType r2(10, 20);
rectangleType r3 = r1 + r2;
std::cout << "r3 length: " << r3.getLength() << std::endl;
std::cout << "r3 width: " << r3.getWidth() << std::endl;
return 0;
}
rectangleType.cpp:
#include "rectangleType.h"
rectangleType operator+(const rectangleType& r1, const rectangleType& r2)
{
rectangleType result;
result.setLength(r1.getLength() + r2.getLength());
result.setWidth(r1.getWidth() + r2.getWidth());
return result;
}
rectangleType.h:
#ifndef H_rectangleType
#define H_rectangleType
#include
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
