Answer to Question #174182 in C++ for Harini.M

Question #174182


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-26T02:18:21-0400
#include <iostream>
using namespace std;
 
class Circle {
  public: 
 
  Circle(){                            // default constructor
     radius = new double; // Dynamically allocate of memory.
     *radius = 1;
  }
 
  Circle(double r){               // parameterized constructor 
     radius = new double;   // Dynamically allocate t of memory.
     *radius = r;
  }
 
  Circle(const Circle& other) { // copy constructor
     radius = new double;
     *radius = *other.radius;
  }
 
  ~Circle() {                       // destructor
     delete radius;            // free resources of data objects
              }
 
  double area(){               // Member function   
     return 3.14 * (*radius) * (*radius);
  }  
  
  private: 
              double* radius;
};
 
// Main function for the program
int main() {
  double radius;
  cout<<"Enter radius : " ;
  cin >> radius;
              
  Circle myCicle(radius); 
  Circle newCicle(myCicle);           //copy constructor
 
  cout<<endl; 
  cout << "Area myCicle = " << myCicle.area() << endl<<endl;
  cout<<"Copy constructor: "<<endl;
  cout << "Area newCicle = " << newCicle.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