1 / 12

Friend Functions and Classes

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

almira
Download Presentation

Friend Functions and Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Friend Functions and Classes M Taimoor Khan taimoorkhan@ciit-attock.edu.pk

  2. 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

  3. 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

  4. 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); }

  5. 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);

  6. 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

  7. 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;

  8. 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

  9. 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

  10. A friend function is what the class trust with its data • Its makes things work a lot easier than doing it with member functions

  11. 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

  12. 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); }

More Related