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 (methods, functions 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,
1 2 3 4 5 6 |
class THuman { public: string name; int age, height; }; |
and if you remember we have created a simple worker object from this THuman class as below,
1 |
THuman worker; |
How we can populate this ? In a simple way we can create workers as below,
1 2 3 |
THuman worker1; THuman worker2; THuman worker3; |
or in a single line as below
1 |
THuman worker1, worker2, worker3; |
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,
1 |
THuman worker[1000]; |
So we can start adding features of of each workers as below,
1 2 3 4 5 6 7 |
worker[0].name = "Kate"; worker[0].age = 35; worker[0].height = 170; worker[1].name = "Tom"; worker[1].age = 24; worker[1].height = 168; |
so on… For example we can easily list all of these worker names, age and height as below,
1 2 3 4 |
for(int i=0; i<1000; i++) { std::cout << worker[i].name << ' | ' << worker[i].age << ' | ' << worker[i].height << '\n'; } |
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.
1 |
THuman *worker; |
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,
1 |
worker = new THuman; |
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;
1 2 3 |
std::cout << "Name:" << worker->name << '\n'; std::cout << "Age:" << worker->age << '\n'; std::cout << "Height:" << worker->height << '\n'; |
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,
1 |
worker->Free(); |
Pointer Object Arrays
If we want to store a lot of members as in pointers we can define array of pointer object as below,
1 |
THuman *worker[1000]; |
Simply, we can allocate all members in the memory as below
1 2 3 4 |
for(int i=0; i<1000; i++) { worker[i] = new THuman; } |
And we can list these members of pointer objects as below;
1 2 3 4 |
for(int i=0; i<1000; i++) { std::cout << worker[i]->name << ' | ' << worker[i]->age << ' | ' << worker[i]->height << '\n'; } |
and at the end, if we done all about using these object we should free them from the memory as below,
1 2 3 4 |
for(int i=0; i<1000; i++) { worker[i]->Free(); } |
In a previous post, we also explained how to use structures.
Get started building powerful apps with C++Builder!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition