Sunday, April 28, 2019

Creating Objects with Object Oriented C++

C++ was one of the first object oriented programming languages decades past, and extended the C programming language with the ability to create classes and derive from base classes, and C++ also introduced the Standard Template Library (STL) for managing class objects. In this blog post we will show how to create an abstract base class, and create a derived class from it, using an example C++ console application that emulates a Zoo.

The console application was created in Visual Studio development environment (IDE).

First we will implement an abstract base class for all animals in the Zoo, called ANIMAL, the common functionality to all animals will be placed in this abstract base class, with a speak() method that will be defined in the derived classes for each specific animal.

The speak() method in the abstract base class will be made a purely virtual method by setting it to 0 in the header file, and also using the VIRTUAL keyword to notate that the method can be overwritten in a derived class. The class will have both a header .h file and implementation .cpp file.

Next, we will create a LION class that will derive from the ANIMAL abstract base class, and will define the speak() method in the LION class to override the base class method with the same name. This class will have both a header .h file and also an implementation .cpp file.

Now we have completed our abstract base class and derived class and are ready to start using these classes in the MAIN method. In C++ there is two different methods to creating class objects, using the NEW keyword, and not using the NEW keyword, called 'in place' object creation. Either method works equally well, but behind the scenes are some important differences.

When creating objects with the NEW keyword, a pointer to the class type is used, and to call the methods, an arrow -> is used. When creating objects without the NEW keyword, a pointer is not used, and methods are called using dot . notation.

When the NEW keyword is used, the object is created in the HEAP of the computer. Whereas if the NEW keyword is not used, called 'in place' object creation, it is instead created on the STACK of the computer. With the NEW keyword dynamic memory is allocated for the object. With 'in place' object creation it is different, automatic memory is used instead.

Using the NEW keyword requires that the pointer to the object be deleted from memory and set to NULL when it is no longer being used, to prevent memory leaks. With 'in place' notation the object is automatically destroyed when the application goes out of scope.

This is different than for other programming languages like C# and JAVA, which have a feature called 'garbage collection' where memory allocations are automatically freed for use without the need to call a DELETE operation manually, like with C++.

The output of this simple C++ program should look like the following:

To see a more detailed example C++ console application to emulate a Zoo and the animals in a Zoo, please visit my GitHub source code repository at