list of objects vs list of pointers in C++

Hi guys, today we will talk about the topic list of objects vs list of pointers in C++.

Before moving to the differences, let us discuss what is a list and what is a linked list.

LIST: A list is also referred to as an array. It is a collection of elements of the same data types. Continuous memory is allocated to all the elements in a list. And hence, random access to any element is possible.

LINKED LIST: A linked list is ordered collection of elements of the same type but they are connected using pointers. So, no continuous memory is allocated. And hence, only sequential access is possible. In other words, if I want to print the third element, then I need to traverse from the first element to the second element and then I will reach the third element.

List Of Objects vs List Of Pointers

Now, let us discuss the difference between a list of objects and a list of pointers.

List Of Object: It means an array that will store the objects of a class.

Example:

#include<bits/stdc++.h>
using namespace std;
class student
{
    int marks;
public:
    void getdata()
    {
         cin>>marks;
    }
    int return_data()
    {
        return marks;
    }
};
int main()
{
    student s[5];            // LINE 1
    int sum;
    for(int i=0;i<5;i++)
    {
        s[i].getdata();
    }
    for(int i=0;i<5;i++)
    {
        sum=sum+s[i].return_data();
    }
    cout<<"sum = "<<sum;
}

In this example, I have created a class student. The student class has data member i.e. marks and 2 member functions getdata and return_data. The getdata function will take the marks of the student from the user. The return_data function will return the marks of the student.

In line1, I have created an array that will store the data of 5 objects of student type.

This program will take marks as input from the user of 5 students and then calculate the sum of marks obtained by all the students.

Let us assume that the user gives the input as:

1 2 3 4 5

So the output for this input will be:

sum = 15

 

List Of Pointers: It means an array that will store the addresses of different variables.

Example,

#include<bits/stdc++.h>
using namespace std;

int main() {
    int *arr[5];          // LINE 1
    int sum=0,a[5];
    for(int i=0;i<5;i++)
    {
        cin>>a[i];
        arr[i]=&a[i];
    }
    for(int i=0;i<5;i++)
    {
        sum=sum+*arr[i];
    }
    cout<<"sum = "<<sum;
}

I have taken an array that will take 5 inputs from the user. In line1, I have created another array, but this array will store the addresses and hence will be called as an array of pointers.

In first for loop, the user will give the input which will be stored in array a, and then its address will be copied in the other array arr.

This program will display the sum of inputs given by the user.

Let us assume that the user gives the input as:

1 3 5 7 9

Then the output for this input will be:

sum = 25

That is all for today, so we learned about the list of objects vs pointers in C++ language.

Leave a Reply

Your email address will not be published. Required fields are marked *