12. Given a class declaration: class Circle Circle double 0, char \"white\") wzi
ID: 3554320 • Letter: 1
Question
12. Given a class declaration: class Circle Circle double 0, char "white") wzid goint (l) const double radius Please define the default constructor, the destructor, the function area and print. In the constructor, you must verify that the input radius is positive and dynamically allocate the memory to color. In the destructor, you must free the memory for the data member color. The formula for the area of a circle is 3.14 radius radius. The print function should output the message: e radius is X and the color is: COLOR where X is a number and COLOR is a string. Then write a main program to drive the class. You can declare one object, call print0 and and output the area. Cir de Cir cle Cd m -oExplanation / Answer
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
Circle(double r = 0, char* c = "white"){
if(r <= 0) {
cout<<"Radius must be positive";
exit(1);
}
color = (char*) malloc(20*sizeof(char));
strcpy (color,c);
}
~Circle(){
delete(color);
}
double area(){
return 3.14*radius*radius;
}
void print() const {
cout << "The radius is " << radius << " and the color is "<< color << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.