Creating Objects with Object Oriented C++

C++ was one of the first object oriented programming languages decades past, introduced in 1985,  and created by the Danish Computer Scientist Bjarne Stroustrup. It 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 1997.  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 Microsoft Visual Studio development environment (IDE).

Concerning the IDE you can use to develop C++ software, if you are using Microsoft Windows, an excellent choice is the Microsoft Visual Studio IDE, which is available for free to individual developers and students using the Community Edition. Also, if you are using Linux or or an Apple Mac  or Macbook computer, another excellent choice from Microsoft is the Visual Studio Code IDE. Code is used by many Angular web application developers, but can also be used for C++ development. Both Visual Studio Community Edition and Visual Studio Code use GIT for source control, and Visual Studio can also work with Azure Devops and Github for source control.

If you are biased against Microsoft, like many open source developers are, you do not have to use Microsoft Visual Studio. An excellent IDE for C/C++ Development is the Eclipse editor, which is written in Java and runs on Microsoft Windows and Unix/Linux equally well, and is also free to use.

If you are using an old computer like Windows XP or Windows 98, or an old Unix computer like with Sun Microsystems Solaris Unix, Hewlett Packard HP-UX Unix, or IBM AIX Unix, An excellent choice to develop C++ applications is using the free GNU GCC compiler which works from the command line. Some Unix computers come with the older gcc compiler by default, but most of them can work with the GCC compiler. Many Solaris Unix computers have a C++ compiler installed by default called CC.

Also an important note, you can use C++ compilers to compile C programs, C++ is backwardly compatible with C, however, it is not possible to compile C++ code for like classes and the STL using a C compiler, C is not upwardly compatible with C++. Just so you know.

For our C++ example console application in this blog post, 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 https://github.com/Michael-G-Workman/ 

Popular Posts