Question

C++ This chapter uses the class rectangleType to illustrate how to overload the operators +, *,...

C++

This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts 1 to 3.

  1. 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 positive.)

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

  3. 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. Similarly, rectangle yard1 is greater than rectangle yard2 if the area of yard1 is greater than the area of yard2. Overload the remaining relational operators using similar definitions.

  4. Write the definitions of the functions to overload the operators defined in steps 1 through 3.

  5. Write a test program that tests various operations on the class rectangleType.

  6. (need to have 3 files !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! main.cpp rectanglType.cpp and rectangleType.h ))))))))) C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I hope this might solve your problem, try to understand, it's pretty easy,

main.cpp

//In my main.cpp file
#include <iostream>
#include "rectangleType.h"

using namespace std;


// part e
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, \nrectangle1: "
<< rectangle1 << endl;

   rectangle4 = ++rectangle3;

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

return 0;
}

rectangleType.cpp

//In my rectangleType.cpp file
#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++()
{
// part d
}

rectangleType rectangleType::operator++(int u)
{
// part d
}

rectangleType rectangleType::operator--()
{
// part d
}

rectangleType rectangleType::operator--(int u)
{
// part d
}

rectangleType rectangleType::operator+
(const rectangleType& rectangle) const
{
// part d
}

rectangleType rectangleType::operator-
(const rectangleType& rectangle) const
{
// part d
}

rectangleType rectangleType::operator*(const rectangleType& rectangle) const
{
// part d
}

bool rectangleType::operator==
(const rectangleType& rectangle) const
{
// part d
}

bool rectangleType::operator!=
(const rectangleType& rectangle) const
{
// part d
}

bool rectangleType::operator<=
(const rectangleType& rectangle) const
{
// part d
}

bool rectangleType::operator<
(const rectangleType& rectangle) const
{
// part d
}

bool rectangleType::operator>=
(const rectangleType& rectangle) const
{
// part d
}

bool rectangleType::operator>
(const rectangleType& rectangle) const
{
// part d
}

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

//In my rectangleType.h file
#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 (part b)
rectangleType operator + (const rectangleType &) const;
rectangleType operator - (const rectangleType &) const;
rectangleType operator * (const rectangleType&) const;

//Overload the increment and decrement operators (part a)
rectangleType operator ++ (); //pre-increment
rectangleType operator ++ (int); //post-increment
rectangleType operator -- (); //pre-decrement
rectangleType operator -- (int); //post-decrement

//Overload the relational operators (part c)
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

Add a comment
Know the answer?
Add Answer to:
C++ This chapter uses the class rectangleType to illustrate how to overload the operators +, *,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e 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...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

  • Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are...

    Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are to create 3 functions: 1. A function to return the perimeter of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the perimeter. 2. A function to return the area of a rectangle. This function shall...

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT