Q:

C++ program to demonstrate example of friend function with class

belongs to collection: C++ Classes and Object programs

0

C++ program to demonstrate example of friend function with class

Friend Function
Friend function is a special type of function, which declares inside the class. Friend function can access the private, protected and public data of the class.
A keyword friend is used before return type of the function declaration/prototype.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Friend function program in C++

/*C++ program to demonstrate example of friend function with class.*/
#include <iostream>
 
using namespace std;
 
class Number
{
private:
    int a;
public:
    void getNum(int x);
    //declaration of friend function
    friend void printNum(Number NUM);
 
     
};
 
//class member function definitions
void Number::getNum(int x)
{
    a=x;
}
 
//friend function definition, no need of class name with SRO (::)
void printNum(Number NUM)
{
    cout << "Value of a (private data member of class Number): " << NUM.a;
 
}
 
int main()
{
    Number nObj; //Object declaration
    nObj.getNum(1000);
    printNum(nObj);
    return 0;
}

Output

    Output:
    Value of a (private data member of class Number): 1000

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Classes and Object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Count the created objects using static member func... >>
<< C++ program to read time in HH:MM:SS format and co...