Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE ANSWER THE FOLLOWING QUESTIONS FOR THE ATTACHED CODE. C++ CODE using namespace std; #include #include Rectangle.h /*----------------------------------------------------------------------------- Name: default constructor for class Rectangle Purpose:

PLEASE ANSWER THE FOLLOWING QUESTIONS FOR THE ATTACHED CODE. C++ CODE

image text in transcribed

using namespace std;

#include

#include "Rectangle.h"

/*----------------------------------------------------------------------------- Name: default constructor for class "Rectangle"

Purpose: Initialize the dimensions of a rectangle to 0.0 -----------------------------------------------------------------------------*/

Rectangle::Rectangle() { Length_ = 0.0; Width_ = 0.0; }

/*----------------------------------------------------------------------------- Name: value constructor for class "Rectangle"

Purpose: Initialize the contents of a rectangle Receive: The length and width of a rectangle -----------------------------------------------------------------------------*/

Rectangle::Rectangle( double Len, double Wid ) { if (Len

if (Wid

/*----------------------------------------------------------------------------- Name: length

Purpose: Return the length of a rectangle Return: The length -----------------------------------------------------------------------------*/

double Rectangle::length() const { return Length_; }

/*----------------------------------------------------------------------------- Name: width

Purpose: Return the width of a rectangle Return: The width -----------------------------------------------------------------------------*/

double Rectangle::width() const { return Width_; }

/*----------------------------------------------------------------------------- Name: perimeter

Purpose: Return the length of the perimeter of a rectangle Return: The length of the perimeter -----------------------------------------------------------------------------*/

double Rectangle::perimeter() const { return 2.0 * Length_ + 2.0 * Width_; }

/*----------------------------------------------------------------------------- Name: area

Purpose: Return the area of a rectangle Return: The area -----------------------------------------------------------------------------*/

double Rectangle::area() const { // INCOMPLETE

return 0.0; }

/*----------------------------------------------------------------------------- Name: is_valid

Purpose: Test whether or not the rectangle is valid Return: The Boolean value for the result of the test -----------------------------------------------------------------------------*/

bool Rectangle::is_valid() const { return Length_ > 0.0 && Width_ > 0.0; }

/*----------------------------------------------------------------------------- Name: write

Purpose: Write a rectangle into the output stream -----------------------------------------------------------------------------*/

void Rectangle::write( ostream& Out ) const { Out

/*----------------------------------------------------------------------------- Name: read

Purpose: Read a rectangle from the input stream -----------------------------------------------------------------------------*/

void Rectangle::read( istream& In ) { char Ch; double Len, Wid;

In >> Ch; if (Ch != '[' || !In.good()) { In.setstate( ios::failbit ); } else { In >> Len >> Ch; if (Ch != ',' || !In.good()) { In.setstate( ios::failbit ); } else { In >> Wid >> Ch; if (Ch != ']' || !In.good()) { In.setstate( ios::failbit ); } else { if (Len

if (Wid

/*----------------------------------------------------------------------------- Name: operator

Purpose: Put a rectangle into an output stream Receive: The output stream The rectangle which is to be written Return: The output stream (for chaining) -----------------------------------------------------------------------------*/

ostream& operator

return Out; }

/*----------------------------------------------------------------------------- Name: operator>>

Purpose: Extract a rectangle from an input stream, where the rectangle is in the form '[ x.x, x.x ]' Receive: The input stream The rectangle which is to be read Return: The input stream (for chaining) -----------------------------------------------------------------------------*/

istream& operator>>( istream& In, Rectangle& Item ) { Item.read( In );

return In; }

/*----------------------------------------------------------------------------- Name: operator==

Purpose: Compare two rectangles for equality Receive: The two rectangles to be compared Return: The Boolean result of the comparison -----------------------------------------------------------------------------*/

bool operator==( const Rectangle& One, const Rectangle& Two ) { return (One.length() == Two.length() && One.width() == Two.width()) || (One.length() == Two.width() && One.width() == Two.length()); }

/*----------------------------------------------------------------------------- Name: operator!=

Purpose: Compare two rectangles for inequality Receive: The two rectangles to be compared Return: The Boolean result of the comparison -----------------------------------------------------------------------------*/

bool operator!=( const Rectangle& One, const Rectangle& Two ) { // INCOMPLETE

return false; }

Part B. ( 0.5 point each) Produce an executable version of the program, then experiment with the program to study its behavior. 1. What happens when the user enters "Ctrl-d" (end-of-file) instead of a properly formatted rectangle? 2. What happens when the user enters an improperly formatted rectangle? Part C. (1 point each) Revise the existing programmer-defined data type, as described below: 1. Some of the operations which are part of type "Rectangle" are implemented only as stubs. Revise your copy of "Rectangle.cpp" to complete the implementation of those functions. 2. Using default argument values, merge the two constructors into a single constructor with the same behavior. 3. Convert functions "length" and "width" into inline functions. 4. Merge functions "write" and "operator>" into a single function named "operator>>", which must be declared as a friend of the class

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

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions