Objects

After implementing a Class, its about time we make use of Box to create Objects in a program.

In this section, 1) Box Class follows the implementation in previous page 2) All codes takes place in main

Objects We create instances of a Class inorder to make use of it. These instances are known as Objects. We can create many objects with just 1 class.

1) Declaring an Object It is similiar to declaring any other data type Syntax: Class Name Variable name ; Example; Box GiftBox(1,2,3); Box ToyBox(2,3,4);

2) Sample code

void main{ int Vol, Area; Box GiftB ox(2, 4, 5);  //Calling of member function to print Volume of box to screen // Vol = GiftBox.Volume; cout << "\nVolume of Dimensionbox is " << Vol<< "\n";

 //Calling of member function to print Surface area of box to screen // Area = GiftBox.SurfaceArea; cout << "\nSurface Area of Dimensionbox is " << Area<< "\n";

Display attributes of GiftBox GiftBox.display; }

3) Explaning Sample Code


 * Box GiftBox(2,4,5) simply creates an object GiftBox with the blueprint Box Class. The constructor is called and initialises Height, Width and Depth to 2,4,5 respectively.


 * To access data members or member functions in an object simply use the dot operator and its name. Eg. GiftBox.Volume to access function Volume in Object GiftBox.