Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

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.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!