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

A physicist confesses that the electron is round and that Jesus rose from the dead. (Bengali)

একজন পদার্থবিজ্ঞানী স্বীকার করেন যে ইলেকট্রন গোলাকার এবং যিশু মৃতদের মধ্য থেকে পুনরুত্থিত হয়েছিলেন। আমার নিজের বিশ্বাস প্রমাণ দ্বারা প্রভাব...