Skip to main content

C++ program to define a class circle to represent circles. Add a data member radius to store the radius of a circle. Write member functions area() and perimeter() to compute the area and perimeter of a circle.

 C++ program to define a class circle to represent circles. Add a data member radius to store the radius of a   circle. Write member functions area() and perimeter() to compute the area and perimeter of a circle.

    To define a Circle class in C++, we start by declaring the class with the class keyword, followed by the name of the class. In this case, we'll call it Circle. We'll then define a private data member to store the radius of the circle. Here's the code to do that:

  class Circle {
    private:
    double radius;
  };
   

    Next, we need to define public member functions that can set and get the value of the radius, as well as calculate the area and perimeter of the circle. Here's the full code for the Circle class:

    
class Circle {
  private:
    double radius;
  public:
    void setRadius(double r) {
        radius = r;
    }
    double getRadius() {
        return radius;
    }
    double area() {
        return 3.14159 * radius * radius;
    }
    double perimeter() {
        return 2 * 3.14159 * radius;
    }
};

       

As you can see, we've defined four public member functions for the Circle class:

  • setRadius() sets the value of the radius to a given value.
  • getRadius() returns the current value of the radius.
  • area() calculates and returns the area of the circle using the formula πr².
  • perimeter() calculates and returns the perimeter of the circle using the formula 2πr.

      

Using the Circle class to calculate area and perimeter

Now that we have defined the Circle class, we can use it to calculate the area and perimeter of a circle of a given radius. Here's the full code for a C++ program that uses the Circle class to calculate the area and perimeter of a circle with radius 5.0:

    
#include 
using namespace std;

class Circle {
  private:
    double radius;
  public:
    void setRadius(double r) {
        radius = r;
    }
    double getRadius() {
        return radius;
    }
    double area() {
        return 3.14159 * radius * radius;
    }
    double perimeter() {
        return 2 * 3.14159 * radius;
    }
};

int main() {
  Circle c;
  c.setRadius(5.0);
  cout << "Radius: " << c.getRadius() << endl;
  cout << "Area: " << c.area() << endl;
  cout << "Perimeter: " << c.perimeter() << endl;
  return 0;
}


       

    In this program, we first create an instance of the Circle class called c. We then set the radius of the circle to 5.0 using the setRadius() function. Finally, we use the getRadius(), area(), and perimeter() functions to print out the radius, area, and perimeter of the circle.

Comments

Popular posts from this blog

C Program to input and display details of students using structure

 C Program to input and display details of students using structure.       In this example, we will enter the details of students like roll number, name and total marks using structure and will be displayed. The total number of student should be entered according to the requirements of the user(maximum value is 50). We can increase the maximum number by changing the statement struct student[50];  to struct student[100];     Here in the output we have entered details of four student and displayed them accordingly. PROGRAM #include <stdio.h> #include <conio.h> struct   student {      int   roll ;      char   name [ 20 ];      int   total ; }; void   main () {      int   i , n ;      struct   student   std [ 50 ];      printf ( "Enter the number of students ...

C Program to input Values in a 2x2 square matrix and display the value

 C Program to input Values in a 2x2 square matrix and display the value     In this example, we will accept values from the user in a 2x2 square and matrix and then it is displayed. To solve this problem we must know what is matrices and arrays. What are Arrays?      An array is a data structure for storing more than one data member of similar data type.          Array can be of following types: One-dimensional Array Two-dimensional Array Multi-dimensional Array             In Array we must always initialize values. PROGRAM #include <stdio.h> #include <conio.h> int   main () {      int   a [ 2 ][ 2 ], i , j ;      printf ( "Enter the values of the matrix:" );      for ( i = 0 ; i < 2 ; i ++)     {          for ( j = 0 ; j < 2 ...

C Program to print the values of the diagonal elements of a square matrix and also find the sum of the values of the diagonal elements

C Program to print the values of the diagonal elements of a square matrix and also find the sum of the values of the diagonal elements.     In this example, we will accept the values of a square matrix and will print the diagonal elements of the of the square matrix, also the sum of the values of the diagonal elements will be printed.     Here, in the output we have taken a 3x3 square matrix.     PROGRAM #include <stdio.h> #include <conio.h> void   main () {      int   a [ 50 ][ 50 ], row , column , i , j , sum = 0 ;      printf ( "Enter the row size of the square matrix : " );      scanf ( " %d " ,& row );      column = row ;      printf ( "As this is a square matrix the row size and column size are equal." );  ...