120 likes | 264 Views
Friend Functions and Classes. M Taimoor Khan taimoorkhan@ciit-attock.edu.pk. Friend Functions. The concept of encapsulation and data hiding dictate that non-member functions should not be able to access an object’s private or protected data
E N D
Friend Functions and Classes M Taimoor Khan taimoorkhan@ciit-attock.edu.pk
Friend Functions • The concept of encapsulation and data hiding dictate that non-member functions should not be able to access an object’s private or protected data • The policy is, if you are not a member, you can not get in • However, there are situations where such rigid discrimination leads to considerable inconvencience
Friends as Bridges • Imagine that you want a function to operate on objects of two different classes • Perhaps the function will take objects of the two classes as arguments, and operate on their private data • In this situation there’s nothing like a friend function
Code Example #include<iostream.h> #include<conio.h> class beta; class alpha{ private: int data1; public: alpha(inti){ data1 = i; } friend intfrFunc(alpha,beta); }; class beta{ private: int data2; public: beta(int j){ data2 = j; } friend intfrFunc(alpha,beta); }; IntfrFunc(alpha a, beta b){ return a.data1+b.data2; } void main(){ alpha aa(4); beta bb(5); cout<<frFunc(aa,bb); }
Explanation • In this program, there are two classes alpha and beta • The constructor in both classes initialize the private data members with the data passed from main function • We want frFunc() function to have access to both these private data members, so we make it a friend function • It is notified with the friend keyword along with function declaration in both the classes friend intfrFunc(alpha, beta);
Cont… • This declaration can be placed anywhere in the class, it doesn’t matter whether its given in the public or private section • An object of each class is passed as an argument to the function frFunc() and it accesses the private data member of both the classes • The function here doesn’t do much and just adds the data items and return the sum • The main function calls this function and prints the results returned back to it
Cont… • Remember that a class can not be referred to until it has been declared • Class beta is referred to in the declaration of the friend function frFunc() in class alpha, so beta class must be declared before we do so class beta;
Controversial Nature • We should note that friend functions are controversial • It goes against the Object oriented basic feature of data encapsulation / data hiding • On the other hand it adds flexibility to the language
How serious is the breach • A friend function must be declared as such within the class whose data it will access • Thus a programmer who does not have access to the source code for the class can not make a function into a friend • In this respect, the integrity of the class is still protected • Friend functions are conceptually messy and potentially lead to a spaghetti-code situation if numerous friend muddy the clear boundaries between classes • For this reason friend functions should be used sparingly
A friend function is what the class trust with its data • Its makes things work a lot easier than doing it with member functions
Friend Classes • The member function of a class can all be made friends at the same time when you make the entire class a friend
Syntax #include<iostream.h> #include<conio.h> class beta; class alpha{ private: int data1; public: alpha(){ data1 = 10; } friend class beta; }; class beta{ public: void func1(alpha a){ cout<<a.data1; } void func2(alpha a){ a.data1++; } }; void main() { alpha a; beta b; b.func1(a); b.func2(a); }