Introduction to Classes in Object Oriented Programming C++

Written by Hamza on. Posted in C++

You may recall i started a  series about the basics of object oriented programming in C++. This is the second article of the series. Here, I give you an introduction to classes in C++, explaining about the basic syntax and structure, giving an example to illustrate just how to instantiate and use an object after declaring a class. 

A class in C++ is basically a collection of some data members of different data types along with the functions that can be performed on them. Once our class is made, it acts like a predefined data type in C++ (like int, char). When you declare a variable using of this class, this declaration is called instantiation and the variable created is called an Object. The advantage of using objects over using simple variables in our code is that objects tend to bring their methods and functionality with them automatically wherever they go. This is a huge help in more advanced C++ programming concepts.

As an example, we are writing a program that deals with a database system in a university that has teachers, students, campuses and classrooms (a limited example). Each of these four basic elements can be made into classes and objects derived from them. Let us make the class for all campuses in our university:


 <a href="" style="font: inherit; color: inherit; box-shadow:none; outline :none; text-decoration : none"></a></p>
<p>class Campus</p>
<p>. private:</p>
<p>.  char * names;</p>
<p>.  int no_of_students;</p>
<p>.  char * disciplines;</p>
<p>. public:</p>
<p>.  get_no_of_students();</p>
<p>.  set_no_of_students(int);</p>
<p>void main()</p>
<p>.  Campus Texas_Campus; //instantiation</p>

(Keep in mind, a class is usually written in a .h file and included in the main .cpp file, a practice I will be explaining in the next article)

As we can see, there are three data members in the class and two member functions. To access any of these in a class, we must first instantiate an object(see main). Then, we can access any member within the class through the ‘ . ‘ operator, i.e, we would write the name of the object, followed by a ‘ . ‘, and then the member which we want to access.  For example, to access the get_no_of_students() function, we would write:  Texas_Campus.get_no_of_students();

Now, there are two things immediately noticeable in the above code. One, the Class notation is differentiated from a normal function notation by the semicolon after its closing bracket. In fact semi colon implies that it is just a declaration. Normally when a class is declared, it is just a template and nothing is created in memory. Two, the class is divided normally into two parts:

1)      The private part

2)      The public part

All the functions declared inside the class can access all the data members, whether private or public. The difference is that the private data members of this class cannot be accessed directly by outside of the class. They must be manipulated using the public part of the class. Herein lies the power of classes and OOP: the security and safety it brings to your code. You can be certain that nothing else in your program can accidentally change a specific data member of an object of a class without you specifically calling a public member function that changes it.

So, if we need to get the number of students in a specific campus, let’s say the Texas Campus in this example, we cannot simply write, let’s say:

<p>cout  <<  Texas.Campus.no_of_students ;</p>

To access it, we would need to call the function get_no_of_students() and the syntax would be:


<p>cout  <<  Texas. get_no_of_students () ;</p>


Similarly, if we need to change the number of students we could pass the new number as a parameter to the set_no_of_students (int).

Now, look at the main function. Here we have declared an object that we have named Texas_Campus of our Campus class. Now, wherever in the program we need to access the Texas Campus’s data we can only access through this object.

Next up is a more detailed look into classes and objects. If you need any help regarding classes or OOP in general, feel free to leave a comment in the comments section below. And stay tuned for the next article in the series!

Tags: , , ,