C++C++11C++14C++17Introduction to C++Learn C++

Learn to Use Object Arrays and Object Pointer Arrays in C++

Object-Oriented Programming is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members.

In a previous post, we described how to define a class and how to create an object from this class. We created a single object for a worker. If I have a lot of workers, how I can create a lot of objects from that class and how I can use object arrays? Classes and objects are very easy and useful to define many objects in an array form. So we can store a lot of variables in their properties and each can have its methods running with its attributes. Let’s remember our simple class definition example for a Human as below,

and if you remember we have created a simple worker object from this THuman class as below,

How we can populate this ? In a simple way we can create workers as below,

or in a single line as below

For small amount objects, this method can be useful, maybe less than 5 objects. If we have many objects we use object arrays, arrays of objects.

Object Arrays

Arrays are stored in memory and we use them to store integers, floating numbers, strings and we reach each of it’s member by using their indexes. In the same way, we can use classes to define object of arrays in numbers. So, how we create object arrays in C++? Objects can be easily used as an array variables. For example as in our example we may have a lot of workers and we can define all of them for 1000 workers like this,

So we can start adding features of of each workers as below,

so on… For example we can easily list all of these worker names, age and height as below,

Pointer Objects

In C++, Objects can be defined as pointers of an object. The pointer is a positive integer number that points to the address of memory blocks. Here are memory blocks will be objects. Thus, in each operation, we can use the only address of an object to reach all properties and methods of this object instead of copying the full properties of an object. In C++, most objects are used as a pointer, this allows functions to run faster. Because we reach the full properties and methods of an object by just using its address data.

Allocating Pointer Objects

If we want set it’s properties and we want to use it’s methods we should allocate this object in the memory as below,

If we want to point another object in memory we don’t need to allocate it, so a developer should know well what kind of pointer he will use.

Reaching to Members of Pointer Objects

In our previous post, we used the dot ‘.’ character to reach each member of objects in memory, i.e. worker.name, worker.age, etc. However, If we want to display the values of each class member of a pointer object, we should use “->” operator. Briefly, “.” is used for static array objects and “->” is used for pointer objects. In Object-Oriented Programming, most objects are pointer objects and this is why we mostly use “->” operators to reach members of these objects. Here is an example;

After using object pointers or generally at the end of our program, when closing or quitting the application, we must free these object allocations from the memory. So how we delete objects from the memory ? Please see example below,

Pointer Object Arrays

If we want to store a lot of members as in pointers we can define array of pointer object as below,

Simply, we can allocate all members in the memory as below

And we can list these members of pointer objects as below;

and at the end, if we done all about using these object we should free them from the memory as below,

In a previous post, we also explained how to use structures.

Get started building powerful apps with C++Builder!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?