Area of circle using friend function in C and C++

 Area of circle using friend function


The area of a circle can be calculated using its mathematical formula i.e. listed below
   
                                               A = π * r * r

         where A = area of circle
          X = value of Pi(π) i.e. ~ 3.1472
              r  = radius of the circle
 
Area of circle using friend function in C and C++


Now the code for area of the circle using friend function in C++ is written below:

#include<iostream.h>

using namespace std;

class AreaOfCircle {

    int radius;

public:

    void get() {

        cout << "Enter the radius of Circle : ";

        cin >> radius;

    }

    friend float calculate (AreaOfCircle ob);

};

 

float calculate (AreaOfCircle ob) {

    return 3.14 * ob.radius * ob.radius;

}

 

int main() {

    AreaOfCircle object;

    object.get();

    cout<<"Area of Circle : "<<calculate(object);

return 0;

}

Sample input :

Enter the radius of Circle : 3

Sample output:

Area of Circle : 28.3248

We cannot write the friend function or the program for area of circle using the friend function in  C, as the friend function is declared using the friend keyword that comes inside the body of the class declared and the class is a topic in C++ not in C language, hence the code for same can't be written in C.👍

Friend function is in C++ not in C-language.

Hope you all get it and try to go for the solution in either C++ or the java.

Next Post Previous Post
No Comment
Add Comment
comment url