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.
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:
1 2 3 | double Length, Width, Height; char Color; float ShoeSize; |
A Class Example in C++
1 2 3 4 5 6 | class ShoeBox { double Length, Width, Height; char Color[12]; float ShoeSize; }; |
The items that compose a class are called members of the class.
And the program that defines a shoe box object could be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> #include <iomanip> #include <string> using namespace std; int main() { // Define the characteristics of a shoe box // The following characteristics are COMPLETELY random double Length(12.55), Width(6.32), Height(8.74); string Color("Yellow Stone"); float ShoeSize = 10.50; // Display the characteristics of the shoe box cout << "Characteristics of this shoe box"; cout << setiosflags(ios::fixed) << setprecision(2); cout << "\n\tLength = " << Length << "\n\tWidth = " << Width << "\n\tHeight = " << Height << "\n\tVolume = " << Length * Width * Height << "\n\tColor = " << Color << "\n\tSize = " << ShoeSize << "\n\n"; return 0; } |
The program would produce:
Characteristics of this shoe box
1 2 3 4 5 6 7 | Length = 12.55 Width = 6.32 Height = 8.74 Volume = 693.22 Color = Yellow Stone Size = 10.50 Press any key to continue... |
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:
1 | class ShoeBox; |
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:
1 | class ClassName{}; |
This could also be created as:
1 2 | class ClassName { }; |
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:
1 2 3 4 5 6 | class ShoeBox { double Length, Width, Height; char Color[12]; float ShoeSize; }; |
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:
1 2 3 4 5 6 7 8 | class ShoeBox { public: double Length, Width, Height; string Color; private: float ShoeSize; }; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 | class ShoeBox { public: double Length, Width, Height; public: string Color; double Volume; private: float ShoeSize; private: char Material; string Color; }; |
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.