Answer to Question #175111 in C++ for KAVIN MUKILAN

Question #175111

Create the class Circle with attribute radius to find the area of circle. Assign the values for data member using copy constructor and display the result. Finally free the resources of data objects using destructor member function. (Note: Area of circle = 3.14 * radius * radius)


1
Expert's answer
2021-03-25T18:22:07-0400

#include <iostream>

using namespace std;

 

class Circle {          

  public: 

// default constructor

  Circle(){

// Dynamically allocate t of memory.

       radius = new double; //attribute

       *radius = 1;

  }

// parameterized constructor 

  Circle(double r){

   // Dynamically allocate t of memory.

       radius = new double;

       *radius = r;

  }

//copy constructor

  Circle(const Circle& other)               {

       radius = new double;

       *radius = *other.radius;

  }

// destructor

// free resources of data objects

  ~Circle() {

       delete radius;

  }

// Member function

  double area();

  

  private:

  double* radius;

};

 

  double Circle::area() {

       return 3.14 * (*radius) * (*radius);

  }

// Main function for the program

int main()

{

  Circle firstCicle(10);       

  Circle secondCicle(firstCicle);      //copy constructor

  Circle thirdCicle(secondCicle);     //copy constructor

    

  cout << "Area firstCicle = " << firstCicle.area() << endl;

  cout << "Area secondCicle = " << secondCicle.area() << endl;

  cout << "Area thirdCicle = " << thirdCicle.area() << endl;

 

  return 0;

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS