Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program that provides an example of passing objects to function using call by reference method

Posted By: Barrett Schmidt     Category: C++ Programming     Views: 2959

Write a program that provides an example of passing objects to function using call by reference method.

Code for Program that provides an example of passing objects to function using call by reference method in C++ Programming

#include<iostream.h>
class data2;
class data1
{
    int a;
    public:
    void get(int no)
    {
        a=no;
    }
    friend void swap(data1 &, data2 &);
    void show()
    {
        cout<<"a= "<<a<<endl;
    }
};

class data2
{
    int b;
    public:
    void get(int no)
    {
        b=no;
    }
    friend void swap(data1 &,data2 &);
    void show()
    {
        cout<<"b= "<<b<<endl;
    }
};
void swap(data1 &a1,data2 &a2)
{
    int temp;
    temp=a1.a;
    a1.a=a2.b;
    a2.b=temp;
}

main()
{
    data1 a;
    data2 b;
    a.get(10);
    b.get(20);
    cout<<"\n\n Before swapping \n\n";
    a.show();
    b.show();
    swap(a,b);
    cout<<"\n\n After swapping\n\n";
    a.show();
    b.show();
}
  
Share: 



Barrett Schmidt
Barrett Schmidt author of Program that provides an example of passing objects to function using call by reference method is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!