Write a program to demonstrate Pass by Reference

/*
  WAP to demonstrate
  Pass by Reference
*/

#include<iostream>
using namespace std;

class Sample
{
int a;

public:
void accept(int x)
{
a=x;
}

void display()
{
cout<<"\n\n A:"<<a;
}
};

int main()
{
Sample p;
Sample &s=p;
//s is reference of p

p.accept(10);
s.display();

cout<<"\n\n Change by using reference:";
s.accept(20);

cout<<"\n\n Displaying using object: ";
p.display();

return 0;
}

Previous
Next Post »