Question: Task E. Creating and deleting objects dynamically In the program 3d-space.cpp, add functions that create and delete coordinate structs dynamically: // allocate memory and initialize
Task E. Creating and deleting objects dynamically
In the program 3d-space.cpp, add functions that create and delete coordinate structs dynamically:
// allocate memory and initialize Coord3D* createCoord3D(double x, double y, double z); // free memory void deleteCoord3D(Coord3D *p);
A usage example:
int main() { double x, y, z; cout << "Enter position: "; cin >> x >> y >> z; Coord3D *ppos = createCoord3D(x,y,z); cout << "Enter velocity: "; cin >> x >> y >> z; Coord3D *pvel = createCoord3D(x,y,z); move(ppos, pvel, 10.0); cout << "Coordinates after 10 seconds: " << (*ppos).x << " " << (*ppos).y << " " << (*ppos).z << endl; deleteCoord3D(ppos); // release memory deleteCoord3D(pvel); } Expected output:
$ ./a.out Enter position: 10 20 30 Enter velocity: 5.5 -1.4 7.77 Coordinates after 10 seconds: 65 6 107.7
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
