C++ Program to find the area of circle using constructor

In this program, You will learn how to find the area of a circle using a constructor in C++.


class Test{
   //statement
};

C++ Program to find the area of circle using constructor

Example: How to find the area of a circle using constructor in C++.

#include<iostream>
using namespace std;

class Test {
public:
    float r, area;

    Test() {
        cout << "Enter radius of a circle:";
        cin >> r;

        area = 3.14 * r * r;
    }

    void display() {
        cout << "Area of circle is: " << area;
    }
};

int main() {

    Test obj;

    obj.display();

    return 0;
}

Output:

Enter radius of a circle:3
Area of circle is: 28.26