Question

In: Computer Science

C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...

C++ Programming: Programming Design and Data Structures

Chapter 13 Ex 2

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.

I need a main.cpp file

Given:

**************rectangleType.cpp********************

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

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

//Decrement the length and width   
assert(length != 0 && width != 0);
length--;
width--;

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

rectangleType rectangleType::operator+
(const rectangleType& rectangle) const
{
rectangleType tempRect;

tempRect.length = length + rectangle.length;
tempRect.width = width + rectangle.width;

return tempRect;
}

rectangleType rectangleType::operator-
(const rectangleType& rectangle) const
{
rectangleType tempRect;

assert(length >= rectangle.length &&
width >= rectangle.width);

tempRect.length = length - rectangle.length;
tempRect.width = width - rectangle.width;

return tempRect;
}

rectangleType rectangleType::operator*(const rectangleType& rectangle) const
{
rectangleType tempRect;

tempRect.length = length * rectangle.length;
tempRect.width = width * rectangle.width;

return tempRect;
}

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

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

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

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

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

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

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

Solutions

Expert Solution

// 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
friend rectangleType operator + (const rectangleType &, const rectangleType &);
friend rectangleType operator - (const rectangleType&, const rectangleType &);
friend rectangleType operator * (const rectangleType&, const rectangleType&);

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

//Overload the relational operators
friend bool operator == (const rectangleType&, const rectangleType&);
friend bool operator != (const rectangleType&, const rectangleType&);
friend bool operator <= (const rectangleType&, const rectangleType&);
friend bool operator < (const rectangleType&, const rectangleType&);
friend bool operator >= (const rectangleType&, const rectangleType&);
friend bool operator > (const rectangleType&, const rectangleType&);


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

protected:
double length;
double width;
};

#endif
//end of rectangleType.h

// rectangleType.cpp


#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 operator ++ (rectangleType &rect)
{
   //increment the length and width
   ++rect.length;
   ++rect.width;
   return rect; //return the incremented value of the object
}

rectangleType operator ++ (rectangleType &rect, int)
{
rectangleType temp = rect; //use this pointer to copy
//the value of the object
//increment the length and width
rect.length++;
rect.width++;

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

rectangleType operator -- (rectangleType &rect)
{
//Decrement the length and width
assert(rect.length != 0 && rect.width != 0);
--rect.length;
--rect.width;

return rect; //return the incremented value of the object
}

rectangleType operator -- (rectangleType &rect, int)
{
rectangleType temp = rect; //use this pointer to copy
//the value of the object

//Decrement the length and width
assert(rect.length != 0 && rect.width != 0);
rect.length--;
rect.width--;

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

rectangleType operator + (const rectangleType &rect1, const rectangleType &rect2)
{
   rectangleType tempRect;

   tempRect.length = rect1.length + rect2.length;
   tempRect.width = rect1.width + rect2.width;

   return tempRect;
}

rectangleType operator - (const rectangleType &rect1, const rectangleType &rect2)
{
rectangleType tempRect;

assert(rect1.length >= rect2.length && rect1.width >= rect2.width);

tempRect.length = rect1.length - rect2.length;
tempRect.width = rect1.width - rect2.width;

return tempRect;
}

rectangleType operator * (const rectangleType &rect1, const rectangleType &rect2)
{
   rectangleType tempRect;

   tempRect.length = rect1.length * rect2.length;
   tempRect.width = rect1.width * rect2.width;

   return tempRect;
}

bool operator == (const rectangleType &rect1, const rectangleType &rect2)
{
return (rect1.area() == rect2.area());
}

bool operator != (const rectangleType &rect1, const rectangleType &rect2)
{
return (rect1.area() != rect2.area());
}

bool operator <= (const rectangleType &rect1, const rectangleType &rect2)
{
return (rect1.area() <= rect2.area());
}

bool operator < (const rectangleType &rect1, const rectangleType &rect2)
{
return (rect1.area() < rect2.area());
}

bool operator >= (const rectangleType &rect1, const rectangleType &rect2)
{
return (rect1.area() >= rect2.area());
}

bool operator > (const rectangleType &rect1, const rectangleType &rect2)
{
return (rect1.area() > rect2.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;
}

//end of rectangleType.cpp

// main.cpp : C++ program to test various operations of Rectangle


#include "rectangleType.h"
#include <iostream>

using namespace std;

int main()
{
   rectangleType r1(10,20), r2(5,15);
   cout<<"R1 ==> Area : "<<r1.area()<<" Perimeter : "<<r1.perimeter()<<endl;
   cout<<"R2 ==> Area : "<<r2.area()<<" Perimeter : "<<r2.perimeter()<<endl;

   r1++;
   cout<<"R1 ==> Length : "<<r1.getLength()<<" Width : "<<r1.getWidth()<<endl;
   ++r2;
   cout<<"R2 ==> Length : "<<r2.getLength()<<" Width : "<<r2.getWidth()<<endl;

   rectangleType rAdd = r1 + r2;
   rectangleType rSub = r1 - r2;
   rectangleType rMul = r1 * r2;

   cout<<"RAdd ==> Length : "<<rAdd.getLength()<<" Width : "<<rAdd.getWidth()<<endl;
   cout<<"RSub ==> Length : "<<rSub.getLength()<<" Width : "<<rSub.getWidth()<<endl;
   cout<<"RMul ==> Length : "<<rMul.getLength()<<" Width : "<<rMul.getWidth()<<endl;

   if(r1 > r2)
       cout<<"R1 is greater than R2";
   else if(r1 < r2)
       cout<<"R1 is less than R2";
   else
       cout<<"R1 is equal than R2";

   return 0;
}
//end of main.cpp

Output:


Related Solutions

c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that are the sum of two other elements. Use a selection sort algorithm, discussed in this chapter to sort the array. Instructions and code for Programming Exercise 14 have been included for your convenience. Exercise 14 Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are...
I need this in C# with pseudocode Exercise #2: Design and implement a programming (name it...
I need this in C# with pseudocode Exercise #2: Design and implement a programming (name it EvenOdd) that uses a while loop to determine and print out all even numbers between 50 and 100 on a single line, separated by commas. Then another while loop in the same program to print out all odd numbers between 50 and 100 on a new line, separated by commas. Document your code and properly label the outputs as shown below. Even numbers between...
I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting)...
I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting) to determine the day of your next meeting from today. The program reads from the user an integer value representing today’s day (assume 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday, etc…) and another integer value representing the number of days to the meeting day. The program determines and prints out the meeting day. Format the outputs following the sample...
7. Chapter 13, Question 2: Use the attached data file “Chapter 13 Data Set 1” to...
7. Chapter 13, Question 2: Use the attached data file “Chapter 13 Data Set 1” to answer this question in the book. Do you agree with the author’s conclusion about whether practice time makes a difference? <15 Hours Practice 15-25 Hours Practice More than 25 Hours Practice 58.7 64.4 68 55.3 55.8 65.9 61.8 58.7 54.7 49.5 54.7 53.6 64.5 52.7 58.7 61 67.8 58.7 65.7 61.6 65.7 51.4 58.7 66.5 53.6 54.6 56.7 59 51.5 55.4 54.7 51.5 61.4...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays the number of days in each month. The program’s output should be similar to this: January has 31 days. February has 28 days. March has 31 days. April has 30 days. May has 31 days. June has 30 days. July has 31 days. August has 31 days. September has 30 days. October has 31 days. November has 30 days. December has 31 days. The...
Introduction Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in...
Introduction Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in Java. In both cases you cannot use libraries or packages that contain pre-built data structures, other than built-in support for objects, arrays, references, and pointers. Classes in C++ and Java can represent anything in the real world. This assignment is to write a compiler for Z++ programming language. The Z++ Programming Language Your program will test if an expression entered by the application user...
Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at...
Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at Merks Sales are paid based on an annual salary rather than an hourly wage. However, some employees are paid weekly while others are paid every other week (biweekly). Weekly employees receive 52 paychecks; biweekly employees receive 26 paychecks. The payroll manager wants a program that displays two amounts: an employee’s weekly gross pay and his or her biweekly gross pay. Complete an IPO chart...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date...
Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check...
Chapter 2 Exercise is divided in to 2 sections A and B. Data for this assignment...
Chapter 2 Exercise is divided in to 2 sections A and B. Data for this assignment is under Data files in module Ex2-30-e8.xls (for A) and Ex2-34-e8.xls (for B). See data under modules. A) The following data give the weekly amounts spent on groceries for a sample of households. $271 $363 $159 $ 76 $227 $337 $295 $319 $250 279 205 279 266 199 177 162 232 303 192 181 321 309 246 278 50 41 335 116 100 151...
Modified from Chapter 07 Programming Exercise 5 Original Exercise: Rock, Paper, Scissors Modification Programming Exercise 11...
Modified from Chapter 07 Programming Exercise 5 Original Exercise: Rock, Paper, Scissors Modification Programming Exercise 11 in Chapter 6 asked you to design a program that plays the Rock, Paper, Scissors game. In the program, the user enters one of the three strings—"rock", "paper", or "scissors"—at the keyboard. Add input validation (with a case-insensitive comparison) to make sure the user enters one of those strings only. Modifications: Allow the user to input "r", "p", "s" or the full strings "Rock",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT