Sunday, 29 March 2015

Passing objects by Reference

Accessibility of the objects passed by reference is similar to those passed by value. Modifications carried out on such objects in the called function will also be reflected in the calling function. The method of passing objects as reference parameters to a function is illustrated in the program.

#include <iostream.h>
class ac
{
int acno;
float bal;
public:
void getdata(void)
{
cout<<”Enter the account number for ac1 object”;
cin>>accno;
cout<<”Enter the balance”;
cin>>bal;
}
void setdata(int an)
{
acno = an;
bal = 0;
}
void setdata(int an, float b)
{
acno = an;
bal = b;
}
void display(void)
{
cout<<”Account Number is “<<acno<<endl;
cout<<”Balance is “<<bal<<endl;
}
void moneytransfer(ac &a1, float amt);
};
void ac::moneytransfer(ac &a1, float amt)
{
bal = bal – amt;
a1.bal = al.bal + amt;
}
void main()
{
ac ac1, ac2, ac3;
int tmon;
ac1.getdata();
ac1.setdata(10);
ac3.setdata(20,750.5);
cout<<”Account Information”<<endl;
ac1.display();
ac2.display();
ac3.display();
cout<<”Enter money to transfer”;
cin>>tmon;
cout<<”Updated information about accounts”<<endl;
ac1.display();
ac2.display();
ac3.display();
}

No comments:

Post a Comment