Question Based on the program above, modify it to include the following (a) a me
ID: 3540829 • Letter: Q
Question
Question
Based on the program above, modify it to include the following
(a) a member function that returns the volume of a square object
(b) a default constructor that initializes the length of the object to 2.
(c) a destructor that prints a statement, ex: "destructing object with length 2
Based on the program above, modify it to include the following a member function that returns the volume of a square object a default constructor that initializes the length of the object to 2. a destructor that prints a statement, ex: "destructing object with length 2" in the main function, do the following in order: create an object using the new operator Invoke area() and volume() functions on the object appropriately to print out the values. Delete the object in (i). Create an dynamic array of 5 objects using the new operator Using a for loop, call some functions on each object in (iv), Invoke the set_data(..) function passing the value of the counter + 1 invoke the area() and volume() functions on each object appropriately to print out the values. Delete the object in (iv).Explanation / Answer
#include <iostream>
#include <math.h>
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 pow(length,3); //changed the function to return the volume
}
Square()
{
length=2;
}
~Square()
{
cout<<"destructing object with length "<<length<<endl; //it will now tell which length is destroyed
}
};
int main()
{
int i;
float area, volume;
Square *sq1 = new Square; // create object using new operator
area = sq1->area();
volume = sq1->volume();
cout<<"Area : "<<area<<endl;
cout<<"Volume : "<<volume<<endl;
delete sq1;
Square *sq2 = new Square[5],*temp; // create object using new operator + array
temp=sq2;
for (i=1; i<=5; i++)
{
temp->set_data(i);
cout<<"Area :"<<temp->area()<<endl;
cout<<"Volume :"<<temp->volume()<<endl;
temp=temp+1; //assigning length for all objects seperately using temp
}
delete[] sq2; //deleting the object array
return 0;
}