DEV Community

Mujahida Joynab
Mujahida Joynab

Posted on

How to return object from function in C++

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

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

    Student(int roll, int cls, double gpa)
    {

        this->roll = roll;
        this->cls = cls;

        this->gpa = gpa;
    }
};
Student fun()
{
    Student Arfin(31, 3, 3.09);
    return Arfin;
}
int main()
{
    Student obj = fun();
    cout << obj.roll << " " << obj.cls << " " << obj.gpa << endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

To initialize the function as object we have to write the class name and object name .

Top comments (0)