Page 109 - DCAP404 _Object Oriented Programming
P. 109
Object-oriented Programming
Notes You should see the output as shown below:
Enter the value of x
20
Display the entered number 20
One class can be friendly with another class. Consider two classes, first and second. If the class
first is friendly with the other class second, then the private data members of the class first are
permitted to be accessed by the public members of the class second. But on the other hand, the
public member functions of the class first cannot access the private members of the class second.
Task The friend declaration can be placed anywhere in the class declaration. Explain.
The following program demonstrates how a class one has granted its friendship to the class two.
The class two is declared friendly in the class one.
#include <iostream.h>
#include <conio.h> .
class one
{
friend class two;
private:
int x;
public:
void getdata();
};
class two
{
public:
void disp(one); .
};
inline void one::getdata()
{
cout <<“Enter a number” ;
cin>>x;
}
inline void two::disp(one obj)
{
int x;
102 LOVELY PROFESSIONAL UNIVERSITY