Implementing+a+Class

 Lets get started and implement a class

Remember a Class is a Blue print which allows you to create many objects in your program.

Here is a sample code implementation for a Class named BOX

A class can consist of any combination of the variable types. The variables in the class are referred to as the member variables or data members. A BOX  class might have member variables representing the Width, Height, Depth and so forth. The functions in the class typically manipulate the member variables. They are referred to as member functions or methods of the class. Methods of the BOX  class might include Area    and  Volume.

Following code declares a Class BOX  //** Declaring a Class BOX **// class Box { private: int Height, Width, Depth;  public:  Box(int ht=0, int wth=0, int Dt=0){Height=ht; Width=wth; Depth=Dt;}; // Constructor//   ~Box{ /*Does Nothing*/ };  Destructor

//Setter Functions  int setHeight (int H) { Height = H }; // Reset Height of Box to H  int setWidth (int W) { Width = W }; //Reset Width of Box to W  int setDepth (int D) { Depth = D }; // Reset Depth of Box to D //  int Volume{return (Height*Width*Depth);}; <span style="color: rgb(238, 83, 83);"> Memeber function to calculate volume // <span style="color: rgb(238, 83, 83);"> <span style="font-family: Arial,sans-serif; color: rgb(238, 83, 83);"> int SurfaceArea <span style="color: rgb(238, 83, 83);"> Memeber function to calculate surface area //<span style="font-family: Arial,sans-serif; color: rgb(238, 83, 83);"> { return 2*Height*Width + 2*Width*Depth + 2*Height*Depth;};

void display <span style="font-size: 10pt; font-family: Arial,Helvetica,sans-serif; color: rgb(0, 0, 0);">Member function to display the data members // <span style="font-family: Arial,sans-serif; color: rgb(238, 83, 83);">{ cout << "\n Height : " << Height; cout << "\n Width : " << Width; cout << "\n Depth : " << Depth; }; };

Now lets run through the various components in the code

Overview In the code section, those in Red are the codes and text in Black are the comments. We all know what is a Box, basically it has a square base. To define a Box, we need to know its Height, Width and Depth. Inaddition, we are able to calculate its surface area and volume.

Naturally, Height, Width and Depth are attributes for Box class. We make use of these attributes to calculate its Surface area and Volume therefore they are functions.

How about the others: 1) Constructor When an instance of a class comes into scope, a special function called the constructor executes. In our example above, //Box(int, int, int)// is a constructor. You can declare one or more constructor functions when you declare the class. If you do not declare at least one constructor function, the compiler provides a hidden default constructor function for the class, which may or may not do anything. NOTE: The constructor function always has the same name as the class. 2) Destructor You always define the destructor as well whenever you define a class. The destructor function name is always that of the class with a tilde character(~) as a prefix. Note: There is only one destructor function for a class. It takes no parameters and returns nothing. The purpose of the destructor is to undo whatever the class object has done that needs to be undone. Such as releasing allocated dynamic heap memory.

3) Setter Functions In OOP, programers practice data hiding or encapsulation. As a result, users are not allowed to directly manipulate the data elements at runtime. The only way to access the data elements Height, Width and Depth is via these setter functions. These is make possible at implementation in the class by putting the data elements in private.

4) Other Functions Other Functions include Volume, Surface Area and Display. First 2 is very explicit therefore will not be elaborated. The purpose of the Display function is to facilitate checking of the values of the attributes.

Above is the implementation of a class. How do we create objects and make use of it in your program? Before this, proceed to discussion forum for more.