Answer to Question #185657 in C++ for VASANTH V

Question #185657

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-04-26T05:41:18-0400
#include <iostream>

using namespace std;

class Circle {

   public:
      int getArea( void ){
          return 3.14 * *ptr * *ptr;
      }
      Circle( int r ){
          cout << "Normal Constructor Called" << endl;
          ptr = new int;
          *ptr = r;
      }
      Circle( const Circle &obj){
          cout << "Copy Constructor Called." << endl;
           ptr = new int;
           *ptr = *obj.ptr;
           
      }  
      ~Circle(){
          cout << "Destructor Called" << endl;
          delete ptr;
      }                  

   private:
      int *ptr;
};

void display(Circle obj) {
   cout << "Area of Circle : " << obj.getArea() <<endl;
}
int main() {
   Circle circle(7);

   display(circle);

   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