Lab_4_updated......docx - MUHAMMAD YASEEN REG ID 192001 Submitted to SIR USMAN GHANI EXPERIMENT NO 4 Friend Classes and Friend Functions Objectives \u2022

Lab_4_updated......docx - MUHAMMAD YASEEN REG ID 192001...

This preview shows page 1 - 4 out of 12 pages.

MUHAMMAD YASEEN REG ID : 192001 Submitted to: SIR USMAN GHANI EXPERIMENT NO 4 Friend Classes and Friend Functions Objectives : To understand the concept of making classes and functions friend to other classes for data sharing Equipment required : Visual Studio/Dev C++ Description: Friendship helps in accessing the private data members of class. Thus it enables a class to grant the access to any other class without inheriting that class. If two classes do not fulfill “is-a” relationship and are required to access the private member function of any class it is declared as friend class of that class. Friend functions are also meant for the same purpose. Friend classes All member functions of a class become friends of the friendship-granting class. Friendship-granting class allows the other class to access all the private, protected, public members directly using (.) operator with Friendship-granting class object. Friend Class Example
Image of page 1
class alpha { int data; friend class beta ; // granting friendship public: alpha(): data(99){} }; class beta { public: void func1(alpha a) {cout<<;} void func2(alpha a) {cout<<;} }; Friendship-grant is not commutative. It is not dual side relationship until or unless declared in both the classes separately Example 1: Friend Function This example explains, how friend function modifies private variable of class. #include<iostream> using namespace std; class A{ friend void change(A); // friend function Declaration: Function will take object of class A private: int val; public: A(); }; A::A():val(30){ // Constructor Definition } void change(A a){ // Friend Function Definition outside the class a.val=50; cout<<a.val; } int main(){
Image of page 2
A obj1; // creating object of class A change(obj1);
Image of page 3
Image of page 4

You've reached the end of your free preview.

Want to read all 12 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes