|
A Simple Class |
|
Exercise |
|
|
Write a simple class called Cube. The object should calculate the total
area and the volume based on the side measurement. If the program supplies
a side equal or lower than 0, reset the side to 1. Create an empty
constructor and an empty destructor.
Implement the object in different files. |
#ifndef CUBE_H
#define CUBE_H
class Cube
{
public:
Cube();
~Cube();
void setSide(double s);
double getSide();
double Area();
double Volume();
void Properties();
private:
double Side;
};
#endif
#include <iostream.h>
#include "cube.h"
Cube::Cube()
{
}
Cube::~Cube()
{
}
void Cube::setSide(double s)
{
Side = s <= 0 ? 1 : s;
}
double Cube::getSide()
{
return Side;
}
double Cube::Area()
{
return 6 * Side * Side;
}
double Cube::Volume()
{
return Side * Side * Side;
}
void Cube::Properties()
{
cout << "Characteristics of this cube";
cout << "\nSide = " << getSide();
cout << "\nArea = " << Area();
cout << "\nVolume = " << Volume() << "\n\n";
}
#include "cube.h"
void main()
{
Cube cube;
cube.setSide(-12.55);
cube.Properties();
Cube de;
de.setSide(28.15);
de.Properties();
}
Here is an example of the result:
Characteristics of this cube
Side = 1
Area = 6
Volume = 1
Characteristics of this cube
Side = 28.15
Area = 4754.53
Volume = 22306.7