C++ Object | How to pass and return object from a function in C++ - Learn C++ - C++ Tutorial - C++ programming



 c++-object

Learn c++ - c++ tutorial - c++-object - c++ examples - c++ programs

 pass return object c++
  • You need to create a new constructor to perform this task. But, no additional constructor is needed.
  • learn c++ tutorials - pass by value and pass by reference

    learn c++ tutorials - pass by value vs pass by reference in c++ Example

  • This is because the copy constructor is already built into all classes by default.
  • In C++ programming, objects can be passed to a function in a similar way as structures.
Learn C++ , C++ Tutorial , C++ programming - C++ Language -Cplusplus

How to pass objects to a function?

 pass object function c++

Example 1: Pass Objects to Function

  • C++ program to add two complex numbers by passing objects to a function.
#include <iostream> 
using namespace std;

class Complex
{
    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 addComplexNumbers(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();

    return 0;
}

Output

Enter real and imaginary number respectively:
2
4 
Enter real and imaginary number respectively:
-3
4
Sum = -1+8i

How to return an object from the function?

  • In C++ programming, object can be returned from a function in a similar way as structures.
  • As the return type of function is weight (an object of class weight), a temporary object temp is created within the function for holding return values. These values are accessed as temp kilogram and temp gram by the function. When the control returns to main (), the object temp is assigned to the object w3 in main( ).
 return object function c++
Learn C++ , C++ Tutorial , C++ programming - C++ Language -Cplusplus

Example 2: Pass and Return Object from the Function

  • In this program, the sum of complex numbers (object) is returned to the main() function and displayed.
#include <iostream>
using namespace std;
class Complex
{
    private:
       int real;
       int imag;
    public:
       Complex(): real(0), imag(0) { }
       void readData()
        {
           cout << "Enter real and imaginary number respectively:"<<endl;
           cin >> real >> imag;
        }
        Complex addComplexNumbers(Complex comp2)
        {
            Complex temp;

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

            // imag represents the imag data of object c3 because this function is called using code c3.add(c1,c2);
            temp.imag = imag+comp2.imag;
            return temp;
        }
        void displayData()
        {
            cout << "Sum = " << real << "+" << imag << "i";
        }
};

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

    c1.readData();
    c2.readData();

    c3 = c1.addComplexNumbers(c2);

    c3.displayData();
    
    return 0;
}

Related Searches to C++ Object | How to pass and return object from a function in C++?