Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

using namespace std;

class rectangleType{

//Required variables

int length;

int width;

public:

//Default Constructor

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

//parameterised constructor

rectangleType(int len, int wid){

length = len;

width = wid;

}

//Returns length

int getLength(){

return length;

}

//Returns width

int getWidth(){

return width;

}

//Returns area

int getArea(){

return length*width;

}

//Overloading pre-increment operator

void operator++(){

++length;

++width;

}

//Overloading post-increment operator

void operator++(int){

length++;

width++;

}

//Overloading pre-decrement operator

void operator--(){

if((length-1) < 0){

return;

}

if((width - 1) < 0){

return;

}

--length;

--width;

}

//Overloading post-decrement operator

void operator--(int){

if((length-1) < 0){

return;

}

if((width - 1) < 0){

return;

}

length--;

width--;

}

//Overloading binary minus operator

void operator-(const rectangleType& r1){

if((length - r1.length) < 0){

cout << "Operation cannot be done.";

}

else if((width - r1.width) < 0){

cout << "Operation cannot be done.";

}

else{

cout << "After subtracting dimensions of first rectangle from other rectangle : " << endl;

cout << "Length : " << length - r1.length << " Width : " << width - r1.width << endl;

}

}

//Overloading equals operator

void operator==(const rectangleType& r1){

if((length*width) == (r1.length*r1.width))

cout << "True" << endl;

else

cout << "False" << endl;

}

//Overloading not equals operator

void operator!=(const rectangleType& r1){

if((length*width) != (r1.length*r1.width))

cout << "True" << endl;

else

cout << "False" << endl;

}

//Overloading greater than operator

void operator>(const rectangleType& r1){

if((length*width) > (r1.length*r1.width))

cout << "First rectangle is greater than second rectangle." << endl;

else

cout << "Second rectangle is greater than rectangle." << endl;

}

//Overloading less than operator

void operator<(const rectangleType& r1){

if((length*width) < (r1.length*r1.width))

cout << "First rectangle is less than second rectangle." << endl;

else

cout << "Second rectangle is less than first rectangle." << endl;

}

//Displays rectangle values

void display(){

cout << "Length : " << length << " | Width : " << width << endl;

}

};

int main(){

rectangleType r1(10,15),r2(3,5);

cout << "Rectangle 1 : " ;

r1.display();

cout << "Rectangle 2 : " ;

r2.display();

++r1;++r2;

cout << endl << "After Pre Increment : " << endl;

cout << "-----------------------------------" << endl;

cout << "Rectangle 1 : " ;

r1.display();

cout << "Rectangle 2 : " ;

r2.display();

--r1;--r2;

cout << endl << "After Pre Decrement : " << endl;

cout << "-----------------------------------" << endl;

cout << "Rectangle 1 : " ;

r1.display();

cout << "Rectangle 2 : " ;

r2.display();

r1++;r2++;

cout << endl << "After Post Increment : " << endl;

cout << "-----------------------------------" << endl;

cout << "Rectangle 1 : " ;

r1.display();

cout << "Rectangle 2 : " ;

r2.display();

r1--;r2--;

cout << endl << "After Post Decrement : " << endl;

cout << "-----------------------------------" << endl;

cout << "Rectangle 1 : " ;

r1.display();

cout << "Rectangle 2 : " ;

r2.display();

cout << endl << "Both rectangles are equal : ";

r1 == r2;

cout << endl << "Both rectangles are not equal : ";

r1 != r2;

cout << endl << "Which rectangle is bigger? " << endl;

r1 > r2;

cout << endl << "Which rectangle is smaller? " << endl;

r1 < r2;

return 0;

}

retangluarTypecpp:

#include

#include

#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;

rectangluarType.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

Contact GitHub API Training Shop Blog About

2017 GitHub, Inc. Terms Privacy Security Status Help

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions