I interrupted at create object using new operator, help me solve it. Type the fu
ID: 3540732 • Letter: I
Question
I interrupted at create object using new operator, help me solve it.
Type the full code and using comment // to explain what is my wrong.
Explanation / Answer
#include <iostream>
#include <math.h>
//#include <new>
using namespace std;
class Square
{
private: int length;
public:
void set_data(int x)
{
length = x;
}
int area()
{
return pow(length,2);
}
int volume()
{
return (length*length*length);
}
Square()
{
length=2;
}
Square(int leng):length(leng)
{
cout<<"constructing object with length 2"<<endl;
}
~Square()
{
cout<<"destructing object"<<endl;
}
};
int main()
{
Square * sq1 = new Square(); // create object using new operator
float area, volume;
area = sq1->area();
cout << "Area of square given by " << area << endl;
volume = sq1->volume();
cout << "Volume of square given by " << volume << endl;
delete sq1;
return 0;
}