Thursday, June 27, 2024

Area of the Circle Using OOP Approach

 #include <iostream>

#include <iomanip>


class Circle {

public:

    // Constructor to initialize the radius

    Circle(double radius) {

        this->radius = radius;

    }


    // Method to calculate the area of the circle

    double calculateArea() {

        return 3.14159265359 * radius * radius;  // Assuming Pi to be approximately 3.14159265359

    }


    // Method to calculate the circumference of the circle

    double calculateCircumference() {

        return 2.0 * 3.14159265359 * radius;  // Assuming Pi to be approximately 3.14159265359

    }


private:

    double radius;

};


int main() {

    double radius;


    // Input the radius of the circle

    std::cout << "\n\tArea of the Circle Using OOP Approach\n";

    std::cout << "\n\tEnter the radius of the circle: ";

    std::cin >> radius;


    // Create a Circle object with the provided radius

    Circle circle(radius);


    // Calculate and display the area and circumference

    double area = circle.calculateArea();

    double circumference = circle.calculateCircumference();


    std::cout << "\n\tThe Area of the circle: " <<std::fixed <<std::setprecision(2) << area << std::endl;

    std::cout << "\tThe Circumference of the circle: " <<std::fixed <<std::setprecision(2) <<  circumference << std::endl;

    std::cout << "\n\n\tEnd of Program. Thank you for using this program." << std::endl;

    return 0;

}


No comments:

Post a Comment