The data types we have applied so far to our variables were used to identify individual items. To create more advanced and complete objects, C++ allows you to group these identifiers and create a newly defined object.

An object, such as a CD Player, a printer, a car, etc, is built from assembling various parts. In the same way, C++ allows you to group various variables and create a new object called a class.

What is a Class?

A class is an organisation of data and functions which operate on them. Data structures are called data members and the functions are called member functions, The combination of data members and member functions constitute a data object or simply an object.

Class in C++

Class in C++

Imagine a company that manufactures shoe boxes hires you to write a program that would help design and identify those shoe boxes. A shoe box is recognized for its dimensions (length, width, height), color, and shoe size that a particular box can contain, etc. The variables that characterize such an object could be:

A Class Example in C++

The items that compose a class are called members of the class.

And the program that defines a shoe box object could be:

The program would produce:
Characteristics of this shoe box

Unless dealing with one shoe box, this program would be rudimentary to run for each object. The solution is to create an object called box that groups everything that characterizes the object.

Creating a Class

To create a class, use the class keyword followed by a name for the object. Like any other declared variable, the class declaration ends with a semi-colon. The name of a class follows the rules we have applied so far for variable and function names. To declare a class called ShoeBox, you would type the following:

As a name that represents a group of items, a class has a body that would be used to define the items that compose it. The body of a class starts with an opening curly bracket “{” and ends with a closing one “}”. Therefore, another way to create a class is:

This could also be created as:

Either of these techniques produces the same effect.

Since a class is built from combining other identifiers, you will list each variable inside of the body of the class. Each item that composes the class is represented as a complete variable declared with a data type and a name. As a variable, each declaration must end with a semi-colon.

Continuing with our shoe box object, you could create it using the class as follows:

Accessing a Class

A common object in real life is visibly made of two categories of parts: those you can see or touch and those you do not have access to. The parts you can see or touch are considered visible or accessible. In C++, such parts are referred to as public. Those you cannot see or touch are considered hidden. In C++, such parts are referred to as private. Like objects in real life, a class is made of sections that the other functions or other objects cannot ?see? and those the other objects can access. The other objects of of the program are sometimes referred to as the clients of the object. The parts the client of an object can touch in a class are considered public and the others are private.

When creating a class, you will define which items are public and which ones are private. The items that are public are created in a section that starts with the public keyword followed by a semi-colon. The others are in the private section. If you do not specify these sections, all of the members of a class are considered private. For example, all of the members of the previously defined ShoeBox class are private.

Using the public and private sections, our shoe box object can be created as:

The public and private keywords are referenced by their access level because they control how much access a variable allows. You can create as many public sections or as many private sections as you want. For example, the above class could be created as:

When creating a class with different public and private sections, all of the declared variables under an access level keyword abide by the rules of that access level. The fact that you use different public sections does not by any means warrant different public levels to the variables. A variable declared as public in one public section has the same public level of access as any other variable that is declared in another public section.

A variable that is declared in a class is called a member of the class. Once the class has been defined, you can use it as an individual variable.