Header File: Box.h
#if !defined VolumesH
#define VolumesH
namespace Volumes
{
class TBox
{
public:
TBox(double L = 0, double H = 0, double W = 0);
TBox(const TBox &Box);
~TBox();
void setLength(const double L) { Length = (L <= 0) ? 0 : L; }
double getLength() const { return (Length < 0) ? 0 : Length; }
void setHeight(const double H) { Height = (H <= 0) ? 0 : H; }
double getHeight() const { return (Height < 0) ? 0 : Height; }
void setWidth(const double W) { Width = (W <= 0) ? 0 : W; }
double getWidth() const { return (Width < 0) ? 0 : Width; }
void setDimensions(const double L, const double H, const double W);
double Area() const;
double Volume() const;
TBox Add(const double Value);
TBox Sub(const double Value);
TBox Mul(const double Value);
TBox Div(const double Value);
TBox Add(const TBox& B);
private:
double Length;
double Height;
double Width;
};
}
#endif // VolumesH
|
Source File: Box.cpp
#include "Volumes.h"
namespace Volumes
{
TBox::TBox(double L, double H, double W)
: Length(L), Height(H), Width(W)
{
}
TBox::TBox(const TBox &Box)
: Length(Box.Length),
Height(Box.Height),
Width(Box.Width)
{
}
TBox::~TBox()
{
}
void TBox::setDimensions(const double L, const double H, const double W)
{
setLength(L);
setHeight(H);
setWidth(W);
}
double TBox::Area() const
{
return 2 * ( ( getLength() * getHeight()) +
( getHeight() * getWidth() ) +
( getLength() * getWidth() ) );
}
double TBox::Volume() const
{
return getLength() * getHeight() * getWidth();
}
TBox TBox::Add(const double d)
{
TBox Box;
Box.Length = Length + d;
Box.Height = Height + d;
Box.Width = Width + d;
return Box;
}
TBox TBox::Sub(const double d)
{
TBox Box;
Box.Length = Length - d;
Box.Height = Height - d;
Box.Width = Width - d;
return Box;
}
TBox TBox::Mul(const double d)
{
TBox Box;
Box.Length = Length * d;
Box.Height = Height * d;
Box.Width = Width * d;
return Box;
}
TBox TBox::Div(const double d)
{
if( d == 0 )
return TBox(0.00, 0.00, 0.00);
else
{
TBox Box;
Box.Length = Length / d;
Box.Height = Height / d;
Box.Width = Width / d;
return Box;
}
}
TBox TBox::Add(const TBox& Box)
{
TBox NewBox;
NewBox.Length = NewBox.Length + Box.Length;
NewBox.Height = NewBox.Height + Box.Height;
NewBox.Width = NewBox.Width + Box.Width;
return NewBox;
}
} // namespace Volumes
|
Source File: Main.cpp
#include <iostream>
#include <iomanip>
#include "Volumes.h"
using namespace std;
using namespace Volumes;
void GetDimensions(TBox *Box)
{
double L, H, W;
cout << "Specify the dimensions of the box\n";
cout << "Length: "; cin >> L;
cout << "Height: "; cin >> H;
cout << "Width: "; cin >> W;
Box->setDimensions(L, H, W);
}
void ShowProperties(const TBox *Box)
{
cout << "\nBox Properties";
cout << setiosflags(ios::fixed) << setprecision(2);
cout << "\nLength = " << Box->getLength();
cout << "\nHeight = " << Box->getHeight();
cout << "\nWidth = " << Box->getWidth();
cout << "\nArea = " << Box->Area();
cout << "\nVolume = " << Box->Volume() << "\n";
}
int main()
{
TBox Box(6.42, 2.84, 4.56);
cout << "Box Examples";
Box = Box.Sub(2.15);
ShowProperties(&Box);
return 0;
}
|