/* C++ Classes and Objects Demo

Copyright © Robin Broad 2018

Free software published under GNU GPL

http://www.robinbroad.co.uk/

April 2018

*/

//Preprocessor instructions to the C++ compiler

//Include the standard C input/ output library

#include <iostream>

//Use the standard names in namespace std for the functions in the iostream library (e.g. cout). Without this, the compiler would not recognise the function cout, which we are using in this program

using namespace std;

//Include the standard C maths library. We need it to calculate the square root sqrt() in the lenSpaceDiag() member function of the Cuboid class

#include<math.h>

class Cuboid

/*Class name Cuboid. This class represents the three dimensional mathematical shape rectangular cuboid. It has six rectangular faces, 12 edges and 8 vertices. We will provide member functions for surface area, volume and the length of the space diagonal.

This code is heavily commented to assist students of C++

Author: Robin Broad

C++ Examples

http://www.robinbroad.co.uk/

April 2018

*/

{

//*** Members ***

private:

/*private means accessible only within the class (or by friends of the class); not publicly accessible.

(This is also known as data hiding. This is part of the process of encapsulation; information hiding, data and methods)*/

//Declaring the class members a, b and c.

int a, b, c;

/*Using the standard mathematical notation of a, b and c for the lengths of the sides of the cuboid

int means "of type integer"*/

//***Constructor ***

/*The constructor is a member function with the same name as the class.

It is called whenever an object (class instance) is created*/

public:

//public means these members or member functions can be accessed anywhere, including from outside the class

//The Cuboid class constructor member function. This is passed the cuboid dimensions a, b and c.

Cuboid(int fpa, int fpb, int fpc)

//fpa=function parameter a etc.

{

/*Notifying us that the constructor has been called

The insertion operator << specifies the string to be written to the console by cout

The \n is the newline character*/

cout << "Constructing a cuboid object of dimensions "<<fpa<<" x "<<fpb<<" x "<<fpc<<" units\n";

//Allocate the passed values to the private member variables of the object (class instance)

a = fpa;

b = fpb;

c = fpc;

}

//*** Destructor ***

//The Cuboid class destructor member function. Destructs the object when it is no longer needed.

//The tilde (~) symbol means "not".

~Cuboid()

{

//notifying us that the object is being destructed

cout << "Destructing a cuboid object of dimensions "<<a<<" x "<<b<<" x "<<c<<" units\n";

}

//*** Member Functions ***

//Defining the member functions for the Cuboid class

int surfaceArea()

/*Member function to calculate the surface area of the cuboid object

Returns the answer as an integer. This in an inline member function (i.e. declared within the class)

This could have been declared outside the class (outline member function) using the form:

int Cuboid::surfaceArea() {...}

(where :: represents the scope resolution operator)

*/

{

return 2*(a*b+a*c+b*c);

/*surfaceArea = 2 x (a x b + a x c + b x c)

This is a standard mathematical concept*/

}

int volume()

/*Member function to calculate the volume of the cuboid object

Returns the answer as an integer. This in an inline member function (i.e. declared within the class)

This could have been declared outside the class (outline member function) using the form:

int Cuboid::volume() {...}

(where :: represents the scope resolution operator)

*/

{

return a*b*c;

/*volume=a x b x c

This is a standard mathematical concept*/

}

float lenSpaceDiag()

/*Member function to calculate the length of the space diagonal of the cuboid object

Note: returns the answer as a float as square root may not be an integer

This in an inline member function (i.e. declared within the class)

This could have been declared outside the class (outline member function) using the form:

int Cuboid::lenSpaceDiag {...}

(where :: represents the scope resolution operator)

*/

{

return sqrt(a*a+b*b+c*c);

/*Length of space diagonal = square root(a^2 + b^2 + c^2)

This is a standard mathematical concept*/

}

};

//}; marks the end of the class definition

//*** Main program ***

int main()

{

//Output Header

cout <<"C++ Classes and Objects Demo\nRobin Broad\nMay 2018\n";

/*This program demonstrates classes, objects, constructors, destructors and member functions in C++

Classes - A class is a data type defined by the programmer, in this case Cuboid. The class has members (dimensions a,b and c) and member functions to return useful information about the object. The class describes the structure of all objects of that class.

Objects - An object is an instance of a class. When an object is created (constructed or instantiated), memory is allocated to it.

Constructor - A constructor is a member function used to initialise an object when it is created. The constructor is public and returns no value.

Destructor - A member function used to remove the object from memory when it is no longer needed.

Member functions - class functions that return useful information about objects of the class, in this case volume, surface area etc.*/

//Create a cuboid object of dimensions 4 x 5 x 2

//Objects can contain organised groups of data (in this case the dimensions of the cuboid) and member functions which return useful results about the object

Cuboid cuboidA(4, 5, 2);

//Create a second cuboid object

Cuboid cuboidB(8, 12, 4);

//Create a third cuboid object

Cuboid cuboidC(5, 10, 3);

//Call the member function surfaceArea() of cuboidA to find it's Surface area

cout <<"Surface area of cuboid A is "<<cuboidA.surfaceArea()<<" units squared\n";

//Call the member function volume() of cuboidB to find it's volume

cout <<"Volume of cuboid B is "<<cuboidB.volume()<<" units cubed\n";

//Call the member function lenSpaceDiag() of cuboidC to find the length of it's space diagonal

cout <<"Length of the space diagonal of cuboid C is "<<cuboidC.lenSpaceDiag()<<" units\n";

return 0;

}

/*This program produced the following output when compiled using the GNU GCC C++ compiler from within the Code::Blocks IDE running on an Ubuntu GNU/Linux operating system, by Robin Broad, Newcastle upon Tyne, UK, May 2018:

C++ Classes and Objects Demo

Robin Broad

May 2018

Constructing a cuboid object of dimensions 4 x 5 x 2 units

Constructing a cuboid object of dimensions 8 x 12 x 4 units

Constructing a cuboid object of dimensions 5 x 10 x 3 units

Surface area of cuboid A is 76 units squared

Volume of cuboid B is 384 units cubed

Length of the space diagonal of cuboid C is 11.5758 units

Destructing a cuboid object of dimensions 5 x 10 x 3 units

Destructing a cuboid object of dimensions 8 x 12 x 4 units

Destructing a cuboid object of dimensions 4 x 5 x 2 units

Process returned 0 (0x0) execution time : 0.006 s

Press ENTER to continue.

*/

/*

Copyright © Robin Broad 2018

This program is free software: you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation, either version 3 of the License, or

(at your option) any later version.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

GNU General Public License: https://www.gnu.org/licenses/gpl.html

*/