Loading...
C++ Pass and Return Objects from a Function

C++ Pass and Return Objects from a Function

In this tutorial, we will learn about how to pass an object to a function as an argument and how to return an object from a function with the help of examples.

Passing and Returning Objects

In C++ we can pass class’s objects as arguments and also return them from a function the same way we pass and return other variables. No special keyword or header file is required to do so.

In C++ programming, we can pass objects to a function in a similar manner as passing regular arguments.


Passing an Object as argument

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables.

Syntax:

function_name (object_name)

Example 1: C++ Pass object to a Function

An object can be passed to a function just like we pass structure to a function. Here in class A we have a function disp() in which we are passing the object of class A. Similarly we can pass the object of another class to a function of different class.

#include <iostream>
using namespace std;
class A {
   public:
    int age = 20;
    char ch = 'A';

    // function that has objects as parameters
    void display(A &a) {

       cout << "Age = " << a.age << endl;
       cout << "Character = " << a.ch << endl;

    }
};
int main() {
    A obj;
    obj.display(obj);
    return 0;
}

Output

Age = 20
Character = A

Example 2: C++ Program to calculate the average age of two students

// C++ Program to understand Pass Objects to Function

#include <iostream>
using namespace std;
class Student {
   public:
    int age;

    // constructor to initialize age
    Student(int a) {
        age = a;
    }
};
// function that has objects as parameters
void calculateAverage(Student s1, Student s2) {
    // calculate the average of age of s1 and s2 
    int average = (s1.age + s2.age) / 2;

   cout << "Average Age = " << average << endl;

}
int main() {
    Student student1(10), student2(20);
    // pass the objects as arguments
        calculateAverage(student1, student2);

    return 0;
}

Output

Average age = 15

Here, we have passed two Student objects student1 and student2 as arguments to the calculateAverage() function.


Returning an Object from a Function

In this example we have two functions, the function input() returns the Student object and display() takes Student object as an argument.

Example 3: C++ Return object to a Function

To print the content of a void pointer, we use the static_cast operator. It converts the pointer from void* type to the respective data type of the address the pointer is storing:

#include <iostream>
using namespace std;
class Student {
   public:
    int stId;
    int stAge;
    string stName;

    // In this function we returning the student object
    Student input(int n, int a, string s) {
        Student obj:
        obj.stId = n;
        obj.stAge = a;
        obj.stName = s;
        return obj;
    }
    // In this function we pass object as an argument
    void display(Student obj) {

       cout << "Name = " << onj.stName << endl;
       cout << "Id = " << onj.stId << endl;
       cout << "Age = " << onj.stAge << endl;
    }
};
int main() {
    Student s;
    s = s.input (1005, 20, James)
    s.display(s);
    return 0;
}

Output

Name = James
Id = 1005
Age = 20

Example 4: C++ Return Object from a Function

#include <iostream>
using namespace std;
class Student {
   public:
    int age1, age2;
};
// function that returns object of Student
Student newStudent() {
    Student student;

    // Initialize member variables of Student
    student.age1 = 10;
    student.age2 = 20;

    // print member variables of Student
    cout << "Age of Student 1 = " << student.age1 << endl;
    cout << "Age of Student 2 = " << student.age2 << endl;

    return student;
}
int main() {
    Student student1;

    // Call function
    student1 = newStudent();

    return 0;
}

Output

Age of Student 1 = 15
Age of Student 2 = 20

In this program, we have created a function newStudent() that returns an object of Student class.

We have called newStudent() from the main() method.

// Call function
student1 = newStudent();

Here, we are storing the object returned by the newStudent() method in the student1.


Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Object & Function in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Enumeration.

- Related Topics