Page 112 - DCAP404 _Object Oriented Programming
P. 112
Unit 5: Static Members
int sum(one obj1,two obj2) Notes
{
return(objl.x + obj2.y);
}
void main()
{
one obj11;
two obj22;
obj11.getdata();
obj11.disp();
obj22.getdata();
obj22.disp();
cout<< “the sum of two private data variables x and y is”;
int tot = sum(obj 11,obj22);
cout <<tot;
getche(); .
}
You should see the output as shown below.
Enter the value for x 20
The value of x entered is 20
Enter the value for y 11
the value of y entered is 11
the sum of two private data variables x & y is 31
Notes Note the forward declaration of class second. The non-member function sum() is
declared friendly to class first and class second.
5.4.1 Friend Function in Empty Classes
#include <iostream>
using namespace std;
class MyClass {
int a, b;
public:
MyClass(int i, int j) { a=i; b=j; }
friend int friendFunction(MyClass x); // a friend function
};
LOVELY PROFESSIONAL UNIVERSITY 105