Area Of Circle using Friend Function - C++Program

Definition Friend Function

If a function is friend function of a class, that friend function is not the actual member of the class.But which function has rights to access to all private and protected members (variables and functions).

For Better Understanding,

Area Of Circle Definition

The area of a circle is π (Pi) times the Radius squared, which is written A=πr2.

The area of a circle is written
A=?r2

here,

r = Radius
? = ~3.14

Simple Program for Friend Function

// Area Of Circle using Friend Function - C++Program 
// GCC Compiler 

#include<iostream>

class AreaOfCircle {
    int radius;
public:
    void get() {
        std::cout << "Enter the radius of Circle : ";
        std::cin >> radius;
    }
    friend float calculate(AreaOfCircle ob);
};

float calculate(AreaOfCircle ob) {
    return 3.14 * ob.radius * ob.radius;
}

int main() {
    AreaOfCircle object;
    object.get();
    std::cout<<"\nArea of Circle : "<<calculate(object);
}

Sample Output

Enter the radius of Circle : 5                                                                                                         
                                                                                                                                       
Area of Circle : 78.5