Answer to Question #174200 in C++ for JOTHIBASS S

Question #174200

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-23T12:16:44-0400
#include <iostream>
using namespace std;
 
class Circle {          
  public: 
// default constructor
              Circle(){
                             radius = new double;
                              *radius = 1;
              }
// 
              Circle(double r){
                              radius = new double;
                              *radius = r;
              }
//copy constructor
              Circle(const Circle& other)  {
                              radius = new double;
                              *radius = *other.radius;
              }
// destructor
              ~Circle() {
                       delete radius;
              }
              
  double area();
  
  private: 
              double* radius;
};
 
              double Circle::area() {
               return 3.14 * (*radius) * (*radius);
              }
 
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