FunctionX - Practical Learning Logo

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.


Header File: cube.h
#ifndef CUBE_H#define CUBE_Hclass Cube{public:	Cube();	~Cube();	void setSide(double s);	double getSide();	double Area();	double Volume();	void Properties();private:	double Side;};#endif
Source File cube.cpp
#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";}
Main File: Exo.cpp
#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 cubeSide   = 1Area   = 6Volume = 1Characteristics of this cubeSide   = 28.15Area   = 4754.53Volume = 22306.7

C++ Tutorial Copyright 2001 FunctionX, Inc.