Saturday, October 13, 2018

Friend Function and Friend class in C++

Friend Function:-

A friend function of a class will be the friend of that class. It will not be the member of that class but it has authority to access all the member of that class even they are private or protected. A friend function can be written in any scope of the class like private, protected and public. 

A friend function declaration will be in the class preceded by friend keyword and its declaration will be outside. 

A friend function is not a member function  (not a part of the object) of the class that means it will not be called by the object name. It is called directly by writing the name of the function with brackets. 

Syntax of friend function declaration:- 

friend return-type function-name(class object);

Syntax of friend function definition:-

return-type function name(class object) 

{

           Statements;

}

#include <iostream>
#include<conio.h>
using namespace std;
class pramod
{
private:
    int a,b;
public:
    friend void add1(pramod s);
    void add2()
    {
        cout<<"Hello"<<endl;
    }
    pramod()
    {
        a=10;
        b=20;
    }
};
void add1(pramod s1)
{
    cout<<s1.a<<endl<<s1.b<<endl;
}
int main()
{
    pramod ob1;
    ob1.add2();
    add1(ob1);
}



Friend Class :-

If we want to make class as a friend function then it well be done by writing the class name preceded by keyword friend. 

Like friend function it can also access all the member function as well as data member of that class where it is defined. It can access all of type of data and member function define in any scope like public, protected and private.



 

  

Font and Basefont tag of html in a very easy way.

Difference between font and basefont tag in html:- Font tag is a container tag. Basefont tag is empty tag. We can apply font tag in a ...