Question
Complete the Code: Box.h: #include using namespace::std; #ifndef cBOX_H #define cBOX_H #include Rect2.h class cBox : public cRectangle2 { friend ostream & operator <
Complete the Code:
Box.h:
#include
using namespace::std;
#ifndef cBOX_H
#define cBOX_H
#include "Rect2.h"
class cBox : public cRectangle2 {
friend ostream & operator<<(ostream &, const cBox &);
friend double volume(const cBox &); // gives the volume
// could have been a member function
public:
cBox(double = 1.0, double = 1.0, double = 1.0);
cBox(const cBox &);
~cBox(void);
double perimeter(void) const; // gives the perimeter of the 12 edges
double area(void) const; // gives the total surface area
cBox & setHeight(double l);
double getHeight(void)const;
private:
double height;
};
#endif
rect2.h:
#include
using namespace::std;
#ifndef cRECT2_H
#define cRECT2_H
class cRectangle2 {
friend ostream & operator<<(ostream &, const cRectangle2 &);
public:
cRectangle2(double , double ); // 2-Argument Constructor
cRectangle2(void); // Default Constructor
cRectangle2(const cRectangle2 &); // Copy Constructor
~cRectangle2(void); // Destructor
double perimeter(void)const;
double area(void)const;
cRectangle2 & setWidth(double w);
cRectangle2 & setLength(double l);
double getWidth(void)const;
double getLength(void) const;
private:
double length;
double width;
};
#endif
box.cpp:
// BOX.cpp Revised 11/16/2005 Homework 6
// Student will write the following member and friend functions
// member function definitions for BOX.cpp Box Class
#include
#include
#include
using namespace::std;
#include "Box.h"
extern ofstream Out;
cBox::cBox(double w, double l, double h)
{
Out << setw(60) << "3-Argument Box Constructor called " << endl;
return;
}
// Copy Constructor
cBox::cBox(const cBox & t)
{
Out << setw(60) << "Box Copy constructor called" << endl;
return;
}
cBox::~cBox(void)
{
Out << setw(60) << "Box Destructor called " << endl;
}
double cBox::perimeter(void)const
{
return 1.0;
}
double cBox::area(void)const
{
return 2.0;
}
cBox & cBox::setHeight(double h)
{
return *this;
}
double cBox::getHeight(void) const
{
return 0.0;
}
double volume(const cBox & t)
{
return 0.0;
}
ostream & operator<<(ostream & where, const cBox & t)
{
return where;
}
lab6.cpp:
// homework 6 - Inheritance
// - Write the member and friend functions for class cBox
// - Fill in the blanks for the driver program below
// - YOUR OUTPUT SHOULD MATCH THE OUTPUT SHOWN IN hw6.txt
#include
#include
using namespace::std;
#include "rect2.h"
#include "box.h"
ofstream Out("hw6.txt");
int main()
{
Out.setf(ios::fixed | ios::showpoint); Out << setprecision(1);
cBox b(4.0, 5.0, 9.0);
// Use ONLY SCOPING in this section
Out << "Output using b with rectangle and box member functions ";
Out << "b has length width and height " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The length is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The width is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The height is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The surface area is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The area of the base is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The volume is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The total perimeter of all sides is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The perimeter of the base is " << xxxxxxxxxxxxxxxxxxxx << endl;
// Use ONLY POINTERS in this section
// CAN ONLY USE THE VARIABLES boxPtr and rectPtr in this SECTION
cBox * boxPtr = xxxxxxxxxxxxxxxxxxxxxxxxx ;
cRectangle2 * rectPtr = xxxxxxxxxxxxxxxxxxxxxxxxx ;
Out << " Output using pointers ";
Out << "b as a box is length width and height " << xxxxxxxxxxxxxxxxxxxx << endl
<< "b as a rectangle is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The surface area is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The volume is " << xxxxxxxxxxxxxxxxxxxx << endl
<< "The area of the base is " << xxxxxxxxxxxxxxxxxxxx << endl;
// USE ONLY CASTING in this section - No new variables - Cast the old vairables
// CAN ONLY USE THE VARIABLES b and boxPtr in this SECTION
Out <<" Output using casting ";
Out << "Casting box as rect using b " << xxxxxxxxxxxxxxxxxxxx << endl
<< "Casting box as rect using boxPtr " << xxxxxxxxxxxxxxxxxxxx << endl;
// Use ONLY REFERENCE variables in this section
// give the variable b an alias of r as a rectangle
cRectangle2 & r = xxxxxxxxxxxxxxxxxxxxxxxxx ;
// give the variable b an alias of x as a box
cBox & x = xxxxxxxxxxxxxxxxxxxxxxxxx ;
// CAN ONLY USE THE VARIABLES r and x in this SECTION
Out <<" Output using reference variables ";
Out << "Viewing b as a rectangle with a reference variable "
<< xxxxxxxxxxxxxxxxxxxxxxxxx << endl;
Out << "output the box using the alias x "
<< xxxxxxxxxxxxxxxxxxxxxxxxx << endl;
Out << "The surface area is " << xxxxxxxxxxxxxxxxxxxxxx << endl
<< "The area of the base is " << xxxxxxxxxxxxxxxxxxxxxx << endl;
// Listing addresses of the variables used
Out <<" The memory adresses of the variables used ";
Out << "The address of b, r and x "
<< xxxxxxxxxxxxxxxxxxxxxxxxx
<<" " << xxxxxxxxxxxxxxxxxxxxxxxxx
<< " " << xxxxxxxxxxxxxxxxxxxxxxxxx << endl;
Out << "The address of the pointer variables boxPrt and rectPtr " <<
xxxxxxxxxxxxxxxxxxxxxxxxx << " "
<< xxxxxxxxxxxxxxxxxxxxxxxxx << endl;
Out << " Testing Copy Constructor for the class box ";
cBox c(b);
Out << "c is " << c << endl;
return 0;
}
rect2.cpp:
// RECT.cpp Revised 11/16/2005 Used for Homework 6
// member function definitions for cRectangle2 Class
#include
#include
#include
using namespace::std;
#include "Rect2.h"
extern ofstream Out;
cRectangle2::cRectangle2(double w, double l)
{
setWidth(w).setLength(l);
Out << setw(60) << "2-Argument Rectangle constructor called" << endl;
return;
}
cRectangle2::cRectangle2(void)
{
setWidth(1.0).setLength(1.0);
Out << setw(60) << "Default Rectangle constructor called" << endl;
}
cRectangle2::cRectangle2(const cRectangle2 & t)
{
setWidth(t.getWidth()).setLength(t.getLength());
Out << setw(60) << "Rectangle Copy constructor called" << endl;
return;
}
cRectangle2::~cRectangle2(void)
{
Out << setw(60) << "Rectangle Destructor called " << endl;
}
double cRectangle2::perimeter(void) const
{
return 2.0 * (getWidth() + getLength());
}
double cRectangle2::area(void) const
{
return getWidth() * getLength();
}
cRectangle2 & cRectangle2::setWidth(double w)
{
width = w > 0.0 && w < 20.0 ? w : 1.0;
return *this;
}
cRectangle2 & cRectangle2::setLength(double l)
{
length = l > 0.0 && l < 20.0 ? l : 1.0;
return *this;
}
double cRectangle2::getWidth(void) const { return width; }
double cRectangle2::getLength(void) const { return length; }
ostream & operator<<(ostream & where, const cRectangle2 & t)
{
where << setw(10) << t.getLength() << setw(10) << t.getWidth();
return where;
}
Output should be of form:
2-Argument Rectangle constructor called 3-Argument Box Constructor called Output using b with rectangle and box member functions b has length width and height 5.0 4.0 9.0 The length is 5.0 The width is 4.0 The height is 9.0 The surface area is 202.0 The area of the base is 20.0 The volume is 180.0 The total perimeter of all sides is 72.0 The perimeter of the base is 18.0 Output using pointers b as a box is length width and height 5.0 4.0 9.0 b as a rectangle is 5.0 4.0 The surface area is 202.0 The volume is 180.0 The area of the base is 20.0 Output using casting Rectangle Copy constructor called Casting box as rect using b 5.0 4.0 Casting box as rect using boxPtr 5.0 4.0 Rectangle Destructor called Output using reference variables Viewing b as a rectangle with a reference variable 5.0 4.0 output the box using the alias x 5.0 4.0 9.0 The surface area is 202.0 The area of the base is 20.0 The memory adresses of the variables used The address of b, r and x 0012FEB4 0012FEB4 0012FEB4 The address of the pointer variables boxPrt and rectPtr 0012FEA8 0012FE9C Testing Copy Constructor for the class box Rectangle Copy constructor called Box Copy constructor called c is 5.0 4.0 9.0 Box Destructor called Rectangle Destructor called Box Destructor called Rectangle Destructor called
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started