c++

C++ Objects and Functions

How to pass and return object from a function in C++?

In this tutorial, you will learn to pass objects to a function and return object from a function in C++ programming. In C++p rogramming,objects can be passed to a function in a similar way as structures.

  1. Pass by Value - This creates a shallow local copy of the object in the function scope
  2. Pass by Reference - This passes a reference to the object to the function
  3. Pass by const
  4. Pass by Pointer

How to pass objects to a function?

There are four ways of passing objects to functions. Let's assume you have a class 10 and want to pass it to a function fun, then

1. Pass by Value

This creates a shallow local copy of the object in the function scope. Things you modify here won't be reflected in the object passed to it. For example,

Declaration :-

void fun(X x)

Calling :-

X x
fun(x);
2. Pass by Reference

This passes a reference to the object to the function. Things you modify here will be reflected in the object passed to it. No copy of the object is created. For example,

Declaration :-

void fun(X &x)

Calling :-

X x;
fun(x);
3. Pass by const Reference

This passes a const reference to the object to the function. You cannot modify/reassign the object here directly (you can use it's methods that do so though). This is useful if you want the function to have only a read-only copy of the object. No copy of the object is created. For example,

Declaration :-

void fun(X const *x)

Calling :-

X x;
fun(&x);
4. Pass by const Pointer

This passes a const pointer to the object to the function. You cannot modify/reassign the pointer here. This is useful if you want the function to have only the address of this object in the pointer. No copy of object is created. Forexample,

Declaration :-

void fun(X *x)

Calling :-

X x;
fun(&x);
5. Pass by Pointer

This passes a pointer to the object to the function. This is similar to passing a reference to the object. No copy of object is created. For example,

Declaration :-

void fun(X *x)

Calling :-

X x;
fun(&x);

Example 1: :Pass Objects to Function

C++ program to add two complex numbers by passing objects to a function.

#include <iostream>

using namespace std;

classComplex {
    private:
        int real;
        int imag;
    public:
        Complex() : real(0), imag(0){ }
    void readData() {
        cout << "Enter real and imaginary number respectively: " << endl;
        cin >> real >> imag;
    }
    void add Complex Numbers(Complex comp1, Complex comp2) {
        // real represents the real data of object c3 because this function 
        // is called using code
        c3.add(c1,c2);

        real = comp1.real + comp2.real;
        // imag represents the imag data of object c3 because this 
        // function is called using code
        c3.add(c1,c2);

        imag = comp1.imag + comp2.imag;
    }
    void displaySum() {
        cout << "Sum = " << real << "+" << imag << "i";
    }
};

int main() {
    Complex c1, c2, c3;

    c1.readData();

    c2.readData();

    c3.addComplexNumbers(c1, c2);
    c3.displaySum();

    return0;
}
p Output:
Enter real and imaginary number respectively: 
2
4
Enter real and imaginary number respectively: 
-3
4
Sum = -1+8i