In this tutorial, we’ll explore Object Oriented Programming by learning to create classes and objects in C++. For beginners, C++ is an ideal programming language to learn and apply OOP concepts as it expects most of the memory management and handling stuff to be done at the developer’s end. Dealing with pointers and their arithmetic is the forte of C++ programming and hence it trains the OOP developers to manage large objects in memory and execution; ranging from dynamic memory allocation to garbage collection.

Object Oriented Programming (OOP) is one of the most popular paradigms in the world of programming. As the name indicates, it operates around the concepts of objects/groups and exposes various features to deal with these objects or groups. If you’re new to Object Oriented Programming, make sure to give a read to our detailed guide here

Enough talking, no? Let’s now dive straight into practically implementing classes and objects in C++. 

Creating Classes in C++

In this section, we’ll learn about how can we group data (as attributes) and functions (as methods) to come up with better structure and organization of code which is easy to understand, reusable and less redundant. In Object Oriented Programming, we formally use the word called class for this purpose. A class is responsible to take care of its attributes and methods and effectively manage them as per the application’s flow and requirements. 

C++ provides a reserved keyword class to declare a class. A simple class declaration in C++ is as follows. 

class Tutorial
    // private attributes and methods here
    // protected attributes and methods here
    // public attributes and methods here

The above code piece declares a class by the name Tutorial. If you’re worried about what private, protected and public means, then you may read about them in great detail here. They are just the access modifiers that allow us to encapsulate the class data and ensure privacy. 

Adding Class Members

The class Tutorial is just a declaration and does nothing as of now. Let’s give more sense to this class by adding relevant attributes and methods.

class Tutorial
    // Class attributes
    int numberOfViews; 
    int numberOfReads;
    int numberOfComments;
    // Class methods
    int getNumberOfViews();
    int getNumberOfComments();
    int getNumberOfComments();
    char* viewTutorial();
    void editTutorial();

Did you notice something in the above class definition? The names of the first two class methods are the same as the name of class i.e. Tutorial. These are special kind of functions and we call them constructor and destructor respectively. Note that these methods (constructors and destructors) need no explicit function call. Constructor is automatically called when the object of the class is created whereas the destructor is also automatically called whenever the object is destroyed. 

A constructor as it is defined above is called a default constructor. We can have multiple overloaded versions of constructor (depending upon the requirements) as well. These versions are called overloaded constructors.

This was the simplest level of class definition in C++ using class attributes and class methods. Note that we can also use static attributes and static methods in C++ classes as well. Also, there are different types of classes such as static class, abstract class, partial class, nested class, etc. 

You might be wondering that what are all these types about? I hear you! Well, this is a separate topic in itself and we’ll discuss it in great detail in upcoming tutorials and lessons. 

Creating Objects in C++

In the previous section, we learnt how to declare and define classes in C++. These class definitions are of no use if we don’t use them in our application. The question here is how do we use these classes? Here comes the idea and usage of objects. In order to use any non-static members of a class (as shown above), we create objects of that class. Once we create objects, we get access to all the public and non-static members of the class and we can then use them as per our program’s requirement. 

There are two ways to create an object of a class; statically (stack based) and dynamically (heap based). You may read further about static and dynamic memory allocation of objects here.

Stack-based Object Creation

To create a stack based object, we can use the following convention: <class_name> <variable_name>;

Let’s create an object of the Tutorial class to see how it works!

Tutorial cppTutorial;

The above code piece creates an object of class Tutorial by the name of cppTutorial

Heap-based Object Creation

To create an object using dynamic memory allocation, we use the new keyword with this convention: <class_name>* <variable_name> = new <class_name>(); 

Let’s create an object of the Tutorial class again. This time, dynamically. 

Tutorial* cppTutorial = new Tutorial();

The asterisk (*) with the class name above indicates that the variable cppTutorial does not have the physical object in it but it is actually a pointer to the object (of type Tutorial) which is created somewhere in the heap memory. 

One thing to note here in C++, whenever we create a stack based object, we use a dot (.) with variable name to access the class members. If we dynamically create an object and store it in a pointer variable, we use a forward arrow (->) to access the members of a class.

Key Note

This was all about creating classes and objects in C++. Easy, right? You don’t actually get a true insight to judge your learning unless you do some hands-on activities. So create your own classes and objects for fun, play around with them and make sure to understand each step. Start with implementing some basic classes like that of a Car, Person, Food, etc. Once you’re familiar with the basic structure and syntax, move on to implementing some complex and more meaningful classes such as that of a Calculator. Your thoughts might start from these ideas but must not be limited to them only. Explore.

If you wish to learn more about C++, you can check out our collection of C++ tutorials.