DEV Community

Mujahida Joynab
Mujahida Joynab

Posted on

How to create a dynamic object

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

class Student {
public:
    int roll;
    int clss;
    double gpa;

    Student(int roll, int clss, double gpa) {
        this->roll = roll;
        this->clss = clss;
        this->gpa = gpa;
    }
};

Student* fun() {
    Student* Rima = new Student(2, 5, 5.00);
    return Rima;
}

int main() {
    Student* p = fun();
    cout << p->roll << " " << p->clss << " " << p->gpa << endl;
}


  `
Enter fullscreen mode Exit fullscreen mode

I created a dynamic object with the following details: Roll - 2, Class - 5, GPA - 5.00.

We are storing the object's address in a pointer named Rima. This pointer holds the address of the dynamic object. We return this pointer to the main function and receive it in the pointer p. We then dereference p and print the values.

Top comments (0)