125,932 views
44 votes
44 votes
This Point class has only a working constructor. Implement it by using the initializer list. point.cpp 1 #include "point.h" 2 3 Point::Point() 4 { 5 // body is empty 6 } point.h 1 #ifndef POINT H 2 #define POINT H 3 4 class Point 5 { 6 public: 7 Point(); 8 private: 9 int m x; // 0,0 10 int my; 11 }; 1213 #endif

User Mike Malter
by
2.8k points

1 Answer

13 votes
13 votes

Answer:

The Point class has a default constructor that does not initialize the mx and my member variables. To implement the default constructor using the initializer list, you can add the member variable names to the initializer list and specify their initial values, as shown below:

// point.cpp

#include "point.h"

Point::Point() : mx(0), my(0)

{

// body is empty

}

This implementation of the default constructor initializes the mx and my member variables to 0 using the initializer list. This means that when a Point object is created using this constructor, the mx and my member variables will be initialized to 0 by default.

The updated point.h file is shown below:

// point.h

#ifndef POINT H

#define POINT H

class Point

{

public:

Point();

private:

int mx; // 0,0

int my;

};

#endif

With this implementation, the default constructor for the Point class correctly initializes the mx and my member variables using the initializer list.

User PaoloC
by
2.8k points