Open In App
Related Articles

Creating a Vector of Class Objects in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Prerequisites: 

Class is a user-defined data type that can be accessed by creating objects or instances of that class. A vector is a type of container which can store elements of a similar type.

Vector of Class

The vector of class objects is an example of a custom vector made where we can store multiple class instances.

Example:

Class:  Student -> { roll , name , age , marks }

If we want to data about students then use this –  vector<Student> v;

C++

// C++ Program to create
// a vector of class objects
#include <iostream>
#include <string>
#include <vector>
  
using namespace std;
  
int randomInt(int start, int range)
{
    // A function to generate random numbers
    return (start + rand() % range);
}
  
string randomString(int len)
{
    // A function to generate random strings of length -->
    // "len"
    string str;
    for (int i = 0; i < len; i++) {
        char ch = 'A' + rand() % 26;
        str.push_back(ch);
    }
    return str;
}
  
class Student {
    int roll;
    string name;
    int age;
    int marks;
  
public:
    void getter()
    {
        roll = randomInt(100, 50);
        name = randomString(10);
        age = randomInt(10, 10);
        marks = randomInt(200, 300);
    }
    void disp()
    {
        cout << roll << "\t" << name << "\t" << age << "\t"
             << marks << "\n";
    }
};
  
int main()
{
    // Vector of class objects
    vector<Student> v;
    Student s;
  
    for (int i = 0; i < 10; i++) {
        // getting the random values from
        // functions
        s.getter();
        // inserting objects to vector
        v.push_back(s);
    }
  
    for (int i = 0; i < 10; i++) {
        // displaying object data
        v[i].disp();
    }
  
    return 0;
}

                    

Output
133    WLRBBMQBHC    17    490
109    ZOWKKYHIDD    12    430
112    DXRJMOWFRX    16    411
142    BLDBEFSARC    13    426
141    ECDYGGXXPK    17    436
105    ELLNMPAPQF    14    364
143    OPKMCOQHNW    19    332
110    EWHSQMGBBU    14    378
117    JJIVSWMDKQ    11    365
139    IXMVTRRBLJ    19    327

Vector of class pointer

It can be used for storing the addresses of the objects rather than directly inserting objects directly in a vector. 

Example:

Class: Land = { name };

Every Land has it’s owner if we want to store owner name.

vector<Land *> sites;

We can use iterator which will point to address where owner lives.

Explanation:

vector<Land *> ;

means we can now only insert address of Land object rather than object itself. So, the address of two objects can be anywhere in a memory (not continuous).

Land* s1 = new Land();

this is used as s1 is storing the address of Land which is allocated in heap memory using new Land()

it->name and it

it is iterator used for traversing . it is address of Land and it->name is the owner of Land.

C++

// C++ Program to create
// a vector of class objects
// code for Example 2
#include <iostream>
#include <vector>
  
using namespace std;
  
class Land {
public:
    string name;
};
  
int main()
{
    // vector of class
    vector<Land*> sites;
  
    // Dynamic Memory Allocated
    Land* s1 = new Land();
    s1->name = "abc";
  
    Land* s2 = new Land();
    s2->name = "xyz";
  
    sites.push_back(s1);
    sites.push_back(s2);
  
    for (auto it : sites) {
        cout << "Owner:" << it->name
             << "  Address_of_Land:" << it << endl;
    }
  
    return 0;
}

                    

Output
Owner:abc  Address_of_Land:0x1a9b010
Owner:xyz  Address_of_Land:0x1a9b060


Last Updated : 10 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads