Thursday, January 19, 2023

FRIEND FUNCTION IN C++

 #include<iostream>

using namespace std;


class cl

{

int a;

public:

void input(int x)

{

a=x;

}

void output()

{

cout<<a<<endl;

}

friend void f(cl ); // the function f() is a friend function , which can access the private member of class cl.

};


// The functiin f is not a member function of f(cl ) . For this reason no scope resolution operator is used .


void f(cl ob)

{

cout<<ob.a<<endl; // ob.a is a private member of class cl.

}


int main ()

{

cl ob;

cout<<"sunday"<<endl;

ob.input(100);

cout<<"monday"<<endl;

ob.output();

cout<<"friday"<<endl;

f(ob);

}

No comments:

Post a Comment

Complete Works of Swami Vivekananda [Volume 8,Page - 2069]

  Complete Works of Swami Vivekananda [ Volume 8, Page - 2069] Jesus Christ was God — the Personal God become man. He has manifested Himsel...