Object-as-function-argument-in-C++ .

In this session i have discussed object as function argument in C++ .


Here we will learn how to pass the object as function argument .

Example : Write a program that demonstrate the how to pass object as function argument .

    #include<iostream>
    using namespace std;  
    class A
    {
        int x;
        public :
          void getdata(int a)
          {
                x=a;
          }
          void putdata()
          {
              cout<<"\nx = "<<x;

          }
          void sum(A obj1, A obj2)
          {
              int s=obj1.x+obj2.x;
              cout<<"\n--------\nT = "<<s;
          }
    };
    int main()
    {
            A obj1,obj2,obj3;
            obj1.getdata(10);
            obj2.getdata(20);
            obj1.putdata();
            obj2.putdata();
            obj3.sum(obj1,obj2);
            return 0;
    }
OUTPUT : x = 20
         y = 10
         ------
         T = 40


Welcome to AndroidATPoint.

back image                     next image