118k views
0 votes
Declare and assign pointer myMovingBody with a new MovingBody object. Call myMovingBody's Read() to read the object's fields. Then, call myMovingBody's Print() to output the values of the fields. Finally, delete myMovingBody.#include #include using namespace std;class MovingBody {public:MovingBody();void Read();void Print();~MovingBody();private:double velocity;double duration;};MovingBody::MovingBody() {velocity = 0.0;duration = 0.0;}void MovingBody::Read() {cin >> velocity;cin >> duration;}void MovingBody::Print() {cout << "MovingBody's velocity: " << fixed << setprecision(1) << velocity << endl;cout << "MovingBody's duration: " << fixed << setprecision(1) << duration << endl;}MovingBody::~MovingBody() { // Covered in section on Destructors.cout << "MovingBody with velocity " << velocity << " and duration " << duration << " is deallocated." << endl;}int main() {MovingBody* myMovingBody = nullptr;/* Your code goes here */return 0;

1 Answer

4 votes

Final answer:

Declare and assign a new MovingBody object to a pointer named myMovingBody, call Read() to read fields, call Print() to output fields values, and then delete the object to free memory.

Step-by-step explanation:

To declare and assign a pointer myMovingBody with a new MovingBody object and use its methods, you can follow these steps in the main function:

  1. Declare the pointer and assign it with a new object:
  2. MovingBody* myMovingBody = new MovingBody();
  3. Call the Read() method to input the object's fields:
  4. myMovingBody->Read();
  5. Call the Print() method to output the values of the fields:
  6. myMovingBody->Print();
  7. Delete the object to release memory:
  8. delete myMovingBody;

Make sure to include necessary headers and use the appropriate namespace declaration.

User Xtlc
by
7.1k points