Area of Circle using Friend Function - C++program
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,
- Understanding Friend Function and Characteristics In C++
- Simple Program for Friend Function Using C++ Programming
- Introduction Of Classes ( OOPS )
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
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
- C++ Function Basics — Tutorial — declaring and calling functions in C++.
- Simple Example Program for Function In C++ — More in c function example programs.
- Simple Example Program for Function Find Smallest Number In C++ — More in c function example programs.