Header File: Box.h
#if !defined _BOX_H_
#define _BOX_H_
class TBox
{
public:
// Default constructor
TBox();
// A line
TBox(double L);
// A rectangle
TBox(double L, double H);
// A rectangular parallepiped
TBox(double L, double H, double W);
double TotalArea(const int n = 3) const;
double Volume() const;
void Properties(const int n = 3) const;
private:
double Length;
double Height;
double Width;
};
#endif // _BOX_H_
|
Source File: Box.cpp
#include <iostream>
#include <iomanip>
#include "Box.h"
using namespace std;
TBox::TBox()
: Length(0.00), Height(0.00), Width(0.00)
{
}
// When the object is instantiated with only the length
// assign 0 to the other not used variables
TBox::TBox(double L)
: Length(L), Height(0.00), Width(0.00)
{
}
// When the object is instantiated with two dimensions
// assign 0 to the other not used variable
TBox::TBox(double L, double H)
: Length(L), Height(H), Width(0.00)
{
}
// This is a complete TBox object
TBox::TBox(double L, double H, double W)
: Length(L), Height(H), Width(W)
{
}
double TBox::TotalArea(const int n) const
{
switch(n)
{
case 1:
return 0.00;
case 2:
return Length * Height;
case 3:
return 2 * ( (Length * Height) +
(Height * Width) +
(Length * Width) );
}
return 0.00;
}
double TBox::Volume() const
{
return Length * Height * Width;
}
void TBox::Properties(const int n) const
{
cout << setiosflags(ios::fixed) << setprecision(2);
switch(n)
{
case 1:
cout << "\nLine";
cout << "\nLength = " << Length << "\n";
break;
case 2:
cout << "\nRectangle";
cout << "\nLength = " << Length;
cout << "\nHeight = " << Height;
cout << "\nArea = " << TotalArea(2) << "\n";
break;
case 3:
cout << "\nBox";
cout << "\nLength = " << Length;
cout << "\nHeight = " << Height;
cout << "\nWidth = " << Width;
cout << "\nArea = " << TotalArea(3);
cout << "\nVolume = " << Volume() << "\n";
break;
}
}
|
Source File: Main.cpp
#include <iostream>
#include "Box.h"
using namespace std;
int main()
{
cout << "Shape Properties\n";
// An instance declared using the default constructor
TBox Brick;
Brick.Properties(3);
// A line created from a box
TBox JustALine(40.68);
JustALine.Properties(1);
// A rectangular object
TBox Rectangular(28.20, 18.44);
Rectangular.Properties(2);
// A complete box
TBox Solid(10.44, 10.72, 12.05);
Solid.Properties(3);
return 0;
}
|