Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab13 – Operator Overloading C++ Advance Please only answer if 100%. When pastei

ID: 3698016 • Letter: L

Question

Lab13 – Operator Overloading

C++ Advance

Please only answer if 100%. When pasteing code use text so I may copy and paste into visual studio. The code needs to work 100%, do not answer if incorrrect. Thank you.

Programming Assignment 1:

Use attached class rectangleType

1.Overload the operators +, *, ==, !=, >>, and <<.

2.Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they must be postive.)

3.Overload the binary operator - to subtract the dimensions of one rectangle from the corresponding dimensions of another rectangle. If the resulting dimensions are not positive, output an appropriate message and do not perform the operation.

4.The operators == and != are overloaded by considering the lengths and widths of rectangles. Redefine the functions to overload the relational operator by considering the areas of rectangles as follows: Two rectangles are the same, if they have the same area; otherwise, the rectangles are not the same.

5.Overload the remaining relational operators using similar definitions.

6.Use attached test program that tests various operations on the class rectangleType

Extra credit (5 Points)

Redo Assignment 1 by overloading the operators as nonmembers of the class rectangleType.

code to use:

Lab13_Part1_MainProgram.cpp

#include

#include "rectangleType.h"

using namespace std;

int main()
{
rectangleType rectangle1(10, 5);
rectangleType rectangle2(8, 7);   
rectangleType rectangle3;   
rectangleType rectangle4;   

cout << "rectangle1: " << rectangle1 << endl;   

cout << "rectangle2: " << rectangle2 << endl;

rectangle3 = rectangle1 + rectangle2;
  
cout << "rectangle3: " << rectangle3 << endl;   

rectangle4 = rectangle1 * rectangle2;   
  
cout << "rectangle4: " << rectangle4 << endl;

if (rectangle1 > rectangle2)   
cout << "Area of rectangle1 is greater than the area "
<< "of rectangle2 ." << endl;
else   
cout << "Area of rectangle1 is less than or equal to the area "
<< "of rectangle2 ." << endl;   

   rectangle1++;

   cout << "After increment the length and width of "
       << "rectangle1 by one unit, rectangle1: "
<< rectangle1 << endl;

   rectangle4 = ++rectangle3;

   cout << "New dimension of rectangle3: " << rectangle3 << endl;
   cout << "New dimension of rectangle4: " << rectangle4 << endl;

return 0;
}

rectangleType.h

#ifndef H_rectangleType
#define H_rectangleType
  
#include
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;

//Overload the arithmetic operators

//Overload the increment and decrement operators


//Overload the relational operators

//constructors
rectangleType();
rectangleType(double l, double w);

protected:
double length;
double width;
};

#endif

rectangleTypelmp.cpp


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

Resources that help:

Lab13 Part1 – operator overloading reference

C++ allows a programmer to overload operators such as the arithmetic or comparison operators. This allows a programmer to use these operators in a very natural way with objects of classes that he / she has created. For example

OVERLOAD THE BINARY (ARITHMETIC OR RELATIONAL) OPERATORS AS MEMBER FUNCTIONS

Function Prototype (to be included in the definition of the class):

returnType operator#(const className&) const;

in which # stands for the binary arithmetic or relational operator to be overloaded; returnType is the type of value returned by the function; and className is the name of the class for which the operator is being overloaded.

Function Definition:

returnType className::operator#

(const className& otherObject) const

{

     //algorithm to perform the operation

     return value;

}

OVERLOAD THE BINARY (ARITHMETIC OR RELATIONAL) OPERATORS AS NONMEMBER FUNCTIONS

Function Prototype (to be included in the definition of the class):

friend returnType operator#(const className&, const className&);

Function Definition:

returnType operator#(const className& firstObject,

const className& secondObject)

{

    //algorithm to perform the operation

    return value;

}

OVERLOADING THE STREAM INSERTION OPERATOR (<<)

The general syntax to overload the stream insertion operator,<<, for a class is described next.

Function Prototype (to be included in the definition of the class):

friend ostream& operator<<(ostream&, const className&);

Function Definition:

ostream& operator<<(ostream& osObject, const className& cObject)

{

//local declaration, if any

//Output the members of cObject.

//osObject << . . .

//Return the stream object.

return osObject;

}

OVERLOADING THE STREAM EXTRACTION OPERATOR (>>)

The general syntax to overload the stream extraction operator,>>, for a class is described next.

Function Prototype (to be included in the definition of the class):

friend istream& operator>>(istream&, className&);

Function Definition:

istream& operator>>(istream& isObject, className& cObject)

{

    //local declaration, if any

    //Read the data into cObject.

    //isObject >> . . .

    //Return the stream object.

    return isObject;

}

OVERLOAD THE ASSIGNMENT OPERATOR =

const className& operator=(const className&);

Function Definition:

const className& className::operator= (const className& rightObject)

{

    //local declaration, if any

     if (this != &rightObject) //avoid self-assignment

     {

     //algorithm to copy rightObject into this object

     }

     //Return the object assigned.

     return *this;

}

OVERLOAD THE PRE-INCREMENT OPERATOR ++ AS A MEMBER FUNCTION

Function Prototype (to be included in the definition of the class):

className operator++();

Function Definition:

className className::operator++()

{

     //increment the value of the object by 1

     return *this;

}

OVERLOAD THE PRE-INCREMENT OPERATOR ++ AS A NONMEMBER FUNCTION

Function Prototype (to be included in the definition of the class):

friend className operator++(className&);

Function Definition:

className operator++(className& incObj)

{

       //increment incObj by 1

       return incObj;

}

OVERLOAD THE POST-INCREMENT OPERATOR ++ AS A MEMBER FUNCTION

Function Prototype (to be included in the definition of the class):

className operator++(int);

Function Definition:

className className::operator++(int u)

{

     className temp = *this;     //use this pointer to copy

     //the value of the object

     //increment the object

     return temp; //return the old value of the object

}

OVERLOAD THE POST-INCREMENT OPERATOR ++ AS A NONMEMBER FUNCTION

Function Prototype (to be included in the definition of the class):

friend className operator++(className&, int);

Function Definition:

className operator++(className& incObj, int u)

{

    className temp = incObj; //copy incObj into temp

    //increment incObj

    return temp; //return the old value of the object

}

Explanation / Answer

#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;
//Overload the arithmetic operators
rectangleType& operator+(rectangleType&);
rectangleType& operator-(rectangleType&);
rectangleType& operator*(rectangleType&);

//Overload the increment and decrement operators
   rectangleType operator++(int);
       rectangleType& operator++();
rectangleType operator--(int);
       rectangleType& operator--();

//Overload the relational operators
bool operator <(const rectangleType&);
bool operator >(const rectangleType&);
bool operator ==(const rectangleType&);
bool operator !=(const rectangleType&);
//ostream, istream overloading
friend ostream& operator<<(ostream& os, const rectangleType&);
friend istream& operator>>(istream& is, rectangleType&);
//constructors
rectangleType();
rectangleType(double l, double w);
protected:
double length;
double width;
};
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+(rectangleType& obj)
{
   rectangleType tmp = *this;
   tmp.length = this->length + obj.length;
tmp.width = this->width + obj.width;
   return tmp;
}
rectangleType& rectangleType::operator-(rectangleType& obj)
{
   rectangleType tmp = *this;
   tmp.length = this->length - obj.length;
tmp.width = this->width - obj.width;
   return tmp;
}

rectangleType& rectangleType::operator*(rectangleType& obj)
{
   rectangleType tmp = *this;
   tmp.length = this->length * obj.length;
tmp.width = this->width * obj.width;
   return tmp;
}
rectangleType rectangleType::operator++(int)
{
   rectangleType tmp = *this;
   this->length = (this->length)++;
   this->width=(this->width)++;
   return tmp;
}
rectangleType rectangleType::operator--(int)
{
   rectangleType tmp = *this;
   this->length = (this->length)--;
   this->width=(this->width)--;
   return tmp;
}
rectangleType& rectangleType::operator++()
{
   this->length = (this->length)++;
   this->width=(this->width)++;
  
   return *this;
}
rectangleType& rectangleType::operator--()
{
   this->length = (this->length)--;
   this->width=(this->width)--;
       return *this;
}

bool rectangleType ::operator <(const rectangleType& d)
{
double d1=this->area();
double d2=d.area();
if(d1<d2)
return true;
else
return false;
}
bool rectangleType ::operator >(const rectangleType& d)
{
double d1=this->area();
double d2=d.area();
if(d1>d2)
return true;
else
return false;
}
bool rectangleType ::operator ==(const rectangleType& d)
{
double d1=this->area();
double d2=d.area();
if(d1==d2)
return true;
else
return false;
}
bool rectangleType ::operator !=(const rectangleType& d)
{
double d1=this->area();
double d2=d.area();
if(d1!=d2)
return true;
else
return false;
}

ostream& operator<<(ostream &os, const rectangleType& c) {
   os << "length:"<<c.length<<" " << "width" << c.width << endl;
   return os;
}


istream& operator>>(istream &is, rectangleType& c) {
   is >> c.length >> c.width;
   return is;
}

   int main()
{
rectangleType rectangle1(10, 5);
rectangleType rectangle2(8, 7);   
rectangleType rectangle3;   
rectangleType rectangle4;   
cout << "rectangle1: " << rectangle1 << endl;   
cout << "rectangle2: " << rectangle2 << endl;
rectangle3 = rectangle1 + rectangle2;
  
cout << "rectangle3: " << rectangle3 << endl;   
rectangle4 = rectangle1 * rectangle2;   
  
cout << "rectangle4: " << rectangle4 << endl;
if (rectangle1 > rectangle2)   
cout << "Area of rectangle1 is greater than the area "
<< "of rectangle2 ." << endl;
else   
cout << "Area of rectangle1 is less than or equal to the area "
<< "of rectangle2 ." << endl;   
rectangle1++;
cout << "After increment the length and width of "
<< "rectangle1 by one unit, rectangle1: "
<< rectangle1 << endl;
rectangle4 = ++rectangle3;
cout << "New dimension of rectangle3: " << rectangle3 << endl;
cout << "New dimension of rectangle4: " << rectangle4 << endl;
return 0;
}