33
Total Posts
3
Categories
24/7
Updated

C++

Write a C++ program to create an array of pointers. Invoke functions using array objects.

Published on Dec 22, 2025
#include <iostream>
using namespace std;

class Student {
private:
    int rollNo;

public:
    void setData(int r) {
        rollNo = r;
    }

    void display() {
        cout << "Roll Number: " << rollNo << endl;
    }
};

int main() {
    Student *ptr[3];   // Array of pointers to Student objects

    // Creating objects dynamically
    for (int i = 0; i < 3; i++) {
        ptr[i] = new Student;
        ptr[i]->setData(100 + i);
    }

    // Invoking functions using array of pointers
    cout << "Student Details:\n";
    for (int i = 0; i < 3; i++) {
        ptr[i]->display();
    }

    // Releasing memory
    for (int i = 0; i < 3; i++) {
        delete ptr[i];
    }

    return 0;
}

Documentation

Output

Student Details:
Roll Number: 100
Roll Number: 101
Roll Number: 102

 

 

  • Student *ptr[3]array of pointers

  • new Studentdynamic object creation

  • ptr[i]->function() → calling member function using pointer

  • delete → frees dynamically allocated memory