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 find out the roots of a Quadratic Equation and also test for Real and Complex roots

 C Program to find out the roots of a Quadratic Equation  and also test for Real and Complex roots.       In this example, we will calculate the roots of the quadratic equation by testing for real and complex roots. We can test real and complex roots by checking these conditions : If b 2  −4 ac  < 0, then there are no real roots. If b 2  −4 ac  = 0, then both the roots are equal. If b 2  −4 ac  > 0, then roots are real and distinct.   PROGRAM TO FIND OUT THE ROOTS OF A QUADRATIC EQUATION. #include <stdio.h> #include <conio.h> #include <math.h> int   main () {      int   a , b , c , d ;      float   x , y ;      printf ( "Enter coefficient of x^2,x and constant : " );      scanf ( " %d   %d   %d " ,& a ,& b ,& c );      d = b * b ...

C Program to Check Even or Odd Without Modulous and Bitwise Operator

  C Program to Check Even or Odd Without Modulous and Bitwise Operator                   In this example we will check even or odd without Modulous Operator. To do this program we should know what is even and odd number.                   Even Number: Even numbers are those numbers which are divisible by 2. That means if we divide a number by 2 and if its remainder appears 0 then it is an even number.                                 For Example: 2,4,6,8,10,12,.... are all even numbers.                              ...

C Program to find the sum of the series 1^2+2^2+3^2+…..+n^2

C Program to find the sum of the series  1 2 +2 2 +3 2 +…..+n 2 Enter the end limit at the runtime.       In this example, we calculate the sum of the above mentioned series till the end-limit entered by the user. We have used built-in function pow(x,y) to find the power of the given number.     Here in the output we have entered the value of end-limit as number 7 which gives a sum of 140.    PROGRAM #include <stdio.h> #include <math.h> #include <conio.h> void   main () {      int  n,sum= 0 ,i;      printf ( "Enter the end limit :" );      scanf ( " %d " ,&n);      for (i= 1 ;i<=n;i++)     {         sum=sum+ pow (i, 2 );     }      printf ( "Sum of the series is :  %d " ,sum);  ...

C Program to Generate Multiplication Table according to User Choice.

 C Program to Generate Multiplication Table according to User Choice.     In this program, we will learn how to print a multiplication table according to any positive number entered by the user. This program uses for loop to solve this problem.  C PROGRAM TO GENERATE MULTIPLICATION TABLE: # include < stdio.h > # include < conio.h > int   main () {      int   i , num , res = 0 ;      printf ( "\n Enter the multplication table you want :  " );      scanf ( " %d " ,& num );      printf ( "\n\n\n\t MULTIPLICATION TABLE OF  %d \t" , num );      printf ( "\n -------------------------------------------------- \n" );      for ( i = 1 ; i <= 10 ; i ++)      {          res = num * i ;       ...

C Program to Multiply two Matrices

 C Program to Multiply two Matrices.     In this example, we will calculate the product of the two matrices allowing the user to enter their required number of rows and columns. In matrix multiplication the number of columns, in the first matrix must be equal to the number of  rows in the second matrix, otherwise multiplication is not possible. The product will have the number of rows of the first matrix and the number of columns of the second matrix.     Here in the output we have taken two matrices of order 2x3 and 3x2. The resulting matrix after calculating the product will be of the order 2x2. PROGRAM #include <stdio.h> #include <conio.h> void   main () {      int   row1 , row2 , column1 , column2 , i , j , k ;      int   a [ 10 ][ 10 ], b [ 10 ][ 10 ], mul [ 10 ][ 10 ];      printf ( "Enter the row size of the 1st Matrix :...