Base Class Magic with Object Oriented C++

C++ was one of the first object oriented programming languages coming out in the 1980s, it first appeared in 1985 and was created by Bjarne Strastroup, a computer scientist from Denmark. It is based upon the C programming language, but has added features including the ability to create classes, create base classes and derived classes, and also the use of generics.

As an object oriented programming language, it has the ideals of Abstraction, Encapsulation, Inheritance, and Polymorphism. And is a vast improvement over the previous C programming language.

Base Classes in C++ make working with Arrays, Lists, and Vectors exceedingly simple. These data types are known as CONTAINERS in the Standard Template Library (STL). The STL was first introduced to C++ in 1997. Previously in C development, data structures using the STRUCT keyword would be made and would link to other elements using a pointer to the previous element and a pointer to the following element, called LINKED LISTS. However, C linked lists are difficult to work with and cumbersome.

When I first started working in the Space Shuttle program, I was tasked with working with XWindows/Motif C applications hosted on HP-UX Unix workstations running the Unix Common Desktop Environment (CDE). These applications obtained database data from the Shuttle Data Center using Oracle Pro-C compilers, which let you embed SQL queries inside of C programs. Almost all of these applications use a linked list with the directional pointers. Later, I was involved in the replacement of these XWindows/Motif applications with Java Swing using JDBC for the database connectivity, a significant improvement.

Also while working in the Space Shuttle program, I developed a C console application on HP Unix for processing of launch data from the Shuttle Data Center obtained with a SQL Script, this particular application had over 100 pointer variables in it. I was always scared it would fail in some spectacular way, but it worked well, and the aerospace engineers who used it were happy with the application because it solved some critical launch data needs for them. I was also extensively involved in Unix and Linux shell script development when I worked in the Space Shuttle program.

One interesting feature with C++, is that you can use the base class in a data type and then add objects of the derived class to the data type, even multiple objects of different derived classes can be added to the data structure, as long as they have the same base class, allowing for simplicity and ease of use.

I included some examples here of using Arrays, Lists, and Vectors in C++ in this manner.

To run these examples, make a C++ console application in Visual Studio, or do the equivalent if using an IDE like Eclipse. This example emulates a Zoo and the animals in the Zoo.

Add the base class for the Animal, listed here in the .h header file and also the .cpp implementation file.

Also add these derived classes for LION and GIRAFFE, the two kinds of animals were are going to have in our C++ zoo.

Next we work on our MAIN method where we will instantiate the animal objects and use them with our various data structures. Do not forget to include these include statements.


Our first example will be an array of type ANIMAL, the abstract base class of the animals in our C++ zoo. C++ has a neat feature for arrays in that you can create an Array of type base class, and then you can add any other class objects to the array if those classes use the base class. In this example we create an array of type ANIMAL, and add a LION object, and also add a GIRAFFE object. Then we can loop through the array and call methods on the objects similar to how you would loop through an array in the C programming language.

First we create our LION and GIRAFFE objects using the NEW keyword.

Next, we build the array and then loop through it with a FOR loop, calling methods on the objects in the array with each iteration.


The list is a very useful data type introduced to C++ some years ago in 1997, and is a vast improvement over working with LINKED LISTS in C programming. Just like with Arrays, you can make the type with the list a base class, and then you are able to add objects to the list made with different classes, as long as they share the same base class. Also the LIST data type provides an ITERATOR that can be used to loop through and dereference the objects in the list. Here is an example of the LIST being used in our C++ Zoo console application.


The VECTOR is a C++ data type that works similar to an Array, but you can think of it as an Array on steroids, so to speak. The advantage of using the VECTOR is that it automatically deletes objects and frees up memory allocated during its usage, which is a sort of "garbage collection" type feature. "Garbage Collection" is a feature of programming languages like Java and C# that provides for automatic deletion of objects and freeing up memory allocation, a feature not included in the C++ programming language. Here is an example of a VECTOR with our C++ Zoo console application, Also note that you can loop through vector types with a FOR loop just like with a regular array. And also note that the objects you add to the vector can be different classes, as long as they share the same base class used to declare the vector. This first example of a VECTOR shows how to use if the size of the VECTOR is known ahead of time, similar to a C array.

In this next example, we will demonstrate how the VECTOR works dynamically, where the size changes with each object pushed on it or popped off it. This is a very powerful feature of the Vector type which enables you to not need to know the size of the Vector before creating it.

I find the ability to use base classes with the data types of ARRAY, LIST, and VECTOR to be a useful and powerful feature of the C++ programming language. In my next C++ blog post, I will show some examples of using the Standard Template Library (STL) and Generic types.

For most of the past 8 years I have primarily worked with Microsoft C# programming language with the Microsoft .NET framework, but I like to visit with C++ from time to time since it was the first object oriented programming language I learned and worked with, and also I am receptive to working as a C++ developer again in the future. Also I am involved with device creation using the Arduino Microcontroller, which is an open source Programmable Logic Controller (PLC) which uses embedded software using the C++ programming language.

To see various example applications I have worked on and created, including my C++ Zoo console application, please visit my publicly viewable GitHub account at


Popular Posts