#15-Array of objects and Dynamic Memory Allocation for Array of Objects

Today in this C++ tutorial, we will be learning about the array of objects and dynamic memory allocation for objects and array of objects.

Though, these things are less used; but they are highly useful; in programming.

So, be ready to learn it easily.

Array of objects

Array is a collection of similar data; in a consecutive memory location; and in under a single variable name. for eg-: a[10] is an array that stores data as a[1]….a[10].

Here we started the array index from 1 which may not be the case always.

i.e sometimes array will start from array index 0 i.e a[0].

So, let’s see what types of questions can be asked in our examinations related to this topic.

Q1) With suitable example, explain dynamic memory allocation for object and object array.

So, let’s start programming.

The above question asks us for object and object array. We are asked to do DMA=dynamic memory allocation as per the question.

So, before doing its DMA, it is highly important to know what it is actually.

Here it is array of object.

So, let’s discuss a program that will illustrate the use of array of object.

The source code of the program is given as follows.

So, let’s see how this program functions i.e how do we make use of the array of objects in this program.

Inside the main function, we have created a *cptr; which is equivalent to cptr[size]. After that, we called the complexx[n] which will allocate the memory dynamically. As we studied before that new operator helps to dynamically allocate memory. After that, we took the input for array of objects i.e  cptr[i] as cptr[i].input(). Finally, w displayed its magnitude; and after displaying its magnitude, we actually deleted the cptr variable. It will help to free the memory used in processing of the cptr variable, which is a pointer variable.

Hence, this program output will come as follows.

codenot.PNG

The formatting of the code is left upto you.

Just keep \n before and after every

Cout<<“\n….\n”;

Then, you will do a fine formatting. There is no need to be depressed because you can’t make formatting well.

Though, later you will know that formatting can help you solve bigger problems, but for the time being; just don’t take it seriously.

Alright!

So, let’s discuss about how the program runs. We will see how did we create array of objects right there.

Inside the main function, as you can see; we have created an array of object as

employee e[5].

employee is a class name and e[5] is a way of making array. So, that make a array of object. (Objects are the sons of class). Then, we can see that the input was taken for the array e[i] using the for loop and similarly output was also shown .

That’s only the main theme of the program. Everything else than this is same. I hope the program has been understood till now very well.

DMA for objects and array of objects

The syntax for dynamic memory allocation is as follows.

Class_name *pointer_object_name;

Pointer_object_name=new class_name;

Pointer_object_name=new class_name[size];

Likewise to free memory, our syntax is like below.

Delete pointer_object_name;

Delete[] pointer_object_name;

So, let’s write a program related to this.

The program source code is as follows.

Check the output of the above program, by pasting it in your code::blocks. I am thinkin of not giving outputs of every programs, so that you at least; will try to learn some programs output by yourself.

So, let’s see how this program functions i.e how do we make use of the array of objects in this program.

Inside the main function, we have created a *cptr; which is equivalent to cptr[size]. After that, we called the complexx[n] which will allocate the memory dynamically. As we studied before that new operator helps to dynamically allocate memory. After that, we took the input for array of objects i.e  cptr[i] as cptr[i].input(). Finally, we displayed its magnitude; and after displaying its magnitude, we actually deleted the cptr variable. It will help to free the memory used in processing of the cptr variable, which is a pointer variable.

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*