C++ Friend Functions - easy4tuts.blogspot.com

Hot Contents To Know

Post Top Ad

Your Ad Spot

C++ Friend Functions

 

Friend Functions

A friend function of a class is defined outside that class' scope but it has the  right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows:

किसी class के friend function को उस class के scope से बाहर define किया जाता है, लेकिन उसे class के सभी private और protected members को access करने का अधिकार है। भले ही friend functions के prototype class definition में दिखाई देते हैं, friend function, class का member function नहीं हैं।

एक friend function एक function, function template, या member function, या एक class या class template हो सकता है, इस स्थिति में पूरी class और उसके सभी member friend होते हैं।

किसी function को class के friend के रूप में declare करने के लिए, class definition में function prototype से पहले keyword friend का उपयोग इस प्रकार करें:

class Box

{

double width; public:

double length;

friend void printWidth( Box box ); void setWidth( double wid );

};

To declare all member functions of class ClassTwo as friends of class ClassOne, place a following declaration in the definition of class ClassOne:

class classTwo के सभी member functions को class classOne के friend के रूप में declare करने के लिए, class classOne की definition में निम्नलिखित declaration लिखे:


friend class ClassTwo;


Consider the following program:

#include <iostream>

using namespace std;

class Box

{

    double width;

public:

    friend void printWidth( Box box );

    void setWidth( double wid );

};

// Member function definition

void Box::setWidth( double wid )

{

width = wid;

}

// Note: printWidth() is not a member function of any class.

void printWidth( Box box )

{

/* Because printWidth() is a friend of Box,

it can directly access any member of this class */

cout << "Width of box : " << box.width <<endl;

}

// Main function for the program

int main( )

{

Box box;

 

box.setWidth(10.0);

 

// Use friend function to print the wdith.

printWidth( box );

 

return 0;

}

Q WAP for using friend function?

#include<iostream>

using namespace std;

class Employee

{

    int eid;

    string name;

public:

    Employee(int e,string n)

    {

        this->eid=e;

        this->name=n;

    }

    Employee(const Employee &obj)

    {

        this->eid=obj.eid;

        this->name=obj.name;

    }

    friend void getdata(Employee obj);

    /*{

        cout<<"Id is:-"<<this->eid;

        cout<<"Name is:-"<<this->name;

    }*/

};

void getdata(Employee obj)

{

    cout<<"Id is:-"<<obj.eid;

    cout<<"Name is:-"<<obj.name;

}

int main()

{

    Employee e1(1,"Rakesh Kumar");

    getdata(e1);

    Employee e2=e1;

    getdata(e2);

    Employee e3(e1);

    getdata(e3);

}

 

Q WAP for using a friend class?

#include<iostream>

using namespace std;

class num

{

    int x=9,y=34;

public:

    friend class ss;

    void getdata()

    {

        cout<<"x-"<<x<<endl;

        cout<<"y-"<<y<<endl;

    }

};

int main()

{

    int result;

    num nobj;

    nobj.getdata();

    ss sobj;

    result=sobj.bb(nobj);

    cout<<"Sum-"<<result;

}

 

No comments:

Post a Comment

Post Top Ad

Your Ad Spot