Question

Task:

Overloading and Templates This chapter uses the class rectangleType to illustrate how to overload the operators +, *, , !- ,

Tasks to complete:

Tasks Less than operator works operator works Subtraction works Addition works

------------------------------------------------------------------------------------------------------------------------------------------

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


#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++()
{
// 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

#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

-----------------------------------------------------------------------------------------------------------------------------

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

//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 (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

//rectangleType.cpp

#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++()
{
rectangleType r;
r.length = ++length;
r.width = ++width;
return r;
}

rectangleType rectangleType::operator++(int u)
{
rectangleType r;
r.length = length++;
r.width = width++;
return r;

}

rectangleType rectangleType::operator--()
{
   if(length>0 &&width>0){
rectangleType r;
r.length = --length;
r.width = --width;
return r;}
else cout<<"Not Possible\n";
}

rectangleType rectangleType::operator--(int u)
{
if(length>0 &&width>0){
   rectangleType r;
r.length = length--;
r.width = width--;
return r;
}
else cout<<"Not Possible\n";

}

rectangleType rectangleType::operator+
(const rectangleType& rectangle) const
{
rectangleType r;
r.length = length + rectangle.length;
r.width = width + rectangle.width;
return r;
}

rectangleType rectangleType::operator-
(const rectangleType& rectangle) const
{
   if(length >= rectangle.length&&width >= rectangle.width){

rectangleType r;
r.length = length - rectangle.length;
r.width = width - rectangle.width;
return r;}
else cout<<"Not Possible\n";
}

rectangleType rectangleType::operator*(const rectangleType& rectangle) const
{
rectangleType r;
r.length = length * rectangle.length;
r.width = width * rectangle.width;
return r;
}

bool rectangleType::operator==
(const rectangleType & rectangle) const
{
return this->area() == rectangle.area();
}

bool rectangleType::operator!=
(const rectangleType& rectangle) const
{
return this->area() != rectangle.area();
}

bool rectangleType::operator<=
(const rectangleType& rectangle) const
{
return this->area() <= rectangle.area();
}

bool rectangleType::operator<
(const rectangleType& rectangle) const
{
return this->area() < rectangle.area();
}

bool rectangleType::operator>=
(const rectangleType& rectangle) const
{
return this->area() >= rectangle.area();
}

bool rectangleType::operator>
(const rectangleType& rectangle) const
{
return this->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;
}

//main.cpp

#include "rectangleType.cpp"

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

rectangle4 = ++rectangle3;

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

return 0;
}

//sample output

CAUsers\IshuManish\ Documents mainrectangle.exe rectangle!: Length = 10; Width = 5 ectangle2: Length = 8; Width = 7 ectangle3

Add a comment
Know the answer?
Add Answer to:
Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...
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
  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

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

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

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

  • 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: •...

  • For this lab you will be creating a class representing the shape square. A Square is...

    For this lab you will be creating a class representing the shape square. A Square is a special case of a Rectangle where both sides have the same length. Rectangle has been provided for your use in this lab. A Rectangle has a height and a width as member variables, two constructors, two member functions, area() and perimeter(), and lastly, an input and output operator. Your Square class should publicly inherit from Rectangle. You will need to update the Constructors...

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

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

    The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...

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