Skip to main content

Area of Circle using Friend Function - C++program

1 min read
Share:
On this page (7sections)

About this program

This is an example program in c function example programs. Read the concept first: C++ Function Basics, then study the code and output below.

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  

Learn the concept first, then study the code:

Related Tutorials

Search tutorials