C++ Program to Find Area of Circle using Functions | Class | Function Overloading | Constructor
C++ program to find area of circle using functions, class, function overloading, and constructor.
In this article, you will learn how to make a c++ program to find area of circle using functions, class, function overloading, and constructor.
Example
Enter the radius of the circle::
7
Diameter :: = 14 units
Circumference :: = 43.96 units
Area :: = 153.86 sq. units
You should have knowledge of the following topics in c++ programming to understand this program:
- C++
cin
object - C++
cout
object - C++ Functions
- C++ Oops
- C++ Function overloading
Formula
D = 2 * r
C = 2 * PI * r
A = PI * r2
Where
R = radius of the circle
D = diameter of the circle
C = circumference of the circle
A = area of the circle
There are two ways to solve these problems:
- By using the default value of PI = 3.14
- Second, by using the M_PI constant
In this article, we solve this problem in six methods:
- Using the normal calculation
- Using the functions
- Using the Class and Object
- Using the function overloading
- Using the Inline Function
- Using the Constructor
1. Using the default value of PI = 3.14
// C++ Program to Find Area of Circle using the default value
#include <iostream>
using namespace std;
int main() {
float r, d = 0, c = 0, a = 0;
// r is the radius of cicle
// d is the diameter of cicle
// c is the circumference of cicle
// a is the area of cicle
cout << "Enter the radius of the circle::\n";
cin >> r;
// It will calculate the diameter, circumference and area of the circle
d = 2 * r;
c = 2 * 3.14 * r;
a = 3.14 * (r * r);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
return 0;
}
Output
Enter the radius of the circle::
7
Diameter :: = 14 units
Circumference :: = 43.96 units
Area :: = 153.86 sq. units
2. Using the M_PI constant
// C++ Program to Find Area of Circle using the M_PI constant
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float r, d = 0, c = 0, a = 0;
// r is the radius of cicle
// d is the diameter of cicle
// c is the circumference of cicle
// a is the area of cicle
cout << "Enter the radius of the circle::\n";
cin >> r;
// It will calculate the diameter, circumference and area of the circle
d = 2 * r;
c = 2 * M_PI * r;
a = M_PI * (r * r);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
return 0;
}
Explanation
In this given program, we have taken the input 7
the radius of the circle from the user and then applied the standard formula to find the Diameter, Circumference, and Area of the circle.
Then it will return these output values of this program as follows:
- Diameter is
14
units - Circumference is
43.96
units - Area is
153.86
sq. units
C++ Program to Find Area of Circle using Functions
// C++ Program to Find Area of Circle using Functions
#include <iostream>
using namespace std;
// This function will calculate the area of the circle
void CalculateArea(float x) {
float d = 0, c = 0, a = 0;
// d is the diameter of cicle
// c is the circumference of cicle
// a is the area of cicle
// It will calculate the diameter, circumference and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
// It's the driver function
int main() {
float r; // radius of cicle
cout << "Enter the radius of the circle::\n";
cin >> r;
CalculateArea(r);
return 0;
}
Enter the radius of the circle::
16
Diameter :: = 32 units
Circumference :: = 100.48 units
Area :: = 803.84 sq. units
C++ Program to Find Area of Circle using Class and Object
// C++ Program to Find Area of Circle using Class and Object
#include <bits/stdc++.h>
using namespace std;
class Calculation {
public:
// This method will calculate the area of the circle
void CalculateArea(float x) {
float d = 0, c = 0, a = 0;
// d is the diameter of cicle
// c is the circumference of cicle
// a is the area of cicle
// It will calculate the diameter, circumference and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
};
// It's the driver function
int main() {
float r; // radius of cicle
cout << "Enter the radius of the circle::\n";
cin >> r;
// To make the instances of `Calculation` class
Calculation CA;
CA.CalculateArea(r);
return 0;
}
Output
Enter the radius of the circle::
12
Diameter :: = 24 units
Circumference :: = 75.36 units
Area :: = 452.16 sq. units
C++ Program to Find Area of Circle using Function Overloading
// C++ Program to Find Area of Circle using Function Overloading
#include <bits/stdc++.h>
using namespace std;
// This function will calculate the area of the circle
// function with int data type parameter
void CalculateArea(int x) {
float d = 0, c = 0, a = 0;
// d is the diameter of cicle
// c is the circumference of cicle
// a is the area of cicle
// It will calculate the diameter, circumference and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
// function with float data type parameter
void CalculateArea(float x) {
float d = 0, c = 0, a = 0;
// d is the diameter of cicle
// c is the circumference of cicle
// a is the area of cicle
// It will calculate the diameter, circumference and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
// It's the driver function
int main() {
int a = 7;
float b = 10.4;
// call function with int data type parameter
CalculateArea(a);
// call function with float data type parameter
CalculateArea(b);
return 0;
}
Output
Diameter :: = 14 units
Circumference :: = 43.96 units
Area :: = 153.86 sq. units
Diameter :: = 20.8 units
Circumference :: = 65.312 units
Area :: = 339.622 sq. units
C++ Program to Find Area of Circle using inline Function
// C++ Program to Find Area of Circle using inline Function
#include <bits/stdc++.h>
using namespace std;
// This function will calculate the area of the circle
inline void CalculateArea(float x) {
float d = 0, c = 0, a = 0;
// d is the diameter of cicle
// c is the circumference of cicle
// a is the area of cicle
// It will calculate the diameter, circumference and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
// It's the driver function
int main() {
float r; // radius of cicle
cout << "Enter the radius of the circle::\n";
cin >> r;
CalculateArea(r);
return 0;
}
C++ Program to Find Area of Circle using Constructor
// C++ Program to Find Area of Circle using Constructor
#include <bits/stdc++.h>
using namespace std;
class Area {
public:
float d = 0, c = 0, a = 0;
// d is the diameter of cicle
// c is the circumference of cicle
// a is the area of cicle
Area(float x)
{
// It will calculate the diameter, circumference and area of the circle
d = (2 * x);
c = (2 * 3.14 * x);
a = 3.14 * (x * x);
}
void Output()
{
// It will produce the final output
cout << "\n";
cout << "Diameter :: = " << d << " units\n";
cout << "Circumference :: = " << c << " units\n";
cout << "Area :: = " << a << " sq. units\n";
}
};
// It's the driver function
int main() {
float r; // radius of the cicle
cout << "Enter the radius of the circle::\n";
cin >> r;
// It will create the instance of `Area` class
Area AR(r);
AR.Output();
return 0;
}
Output
Enter the radius of the circle::
20
Diameter :: = 40 units
Circumference :: = 125.6 units
Area :: = 1256 sq. units
Also, visit these links
C Program to Enter Two Numbers and Perform All Arithmetic Operations