157k views
3 votes
Write a constructor definition for a class called Location. It should take 2 parameters representing an x, y position (floating point) and uses them to initialize data members named x, y using the initialization section of the constructor and call member function startGps (no parameters).

1 Answer

4 votes

Final answer:

A constructor is a special member function of a class that is used to initialize objects of that class. In this case, we need to write a constructor definition for a class called Location that takes 2 parameters representing an x and y position. The constructor should use these parameters to initialize the data members named x and y using the initialization.

Step-by-step explanation:

Answer:

A constructor is a special member function of a class that is used to initialize objects of that class. In this case, we need to write a constructor definition for a class called Location that takes 2 parameters representing an x and y position. The constructor should use these parameters to initialize the data members named x and y using the initialization section of the constructor.

Here is the constructor definition for the Location class:

class Location {
private:
double x;
double y;

public:
Location(double posX, double posY) : x(posX), y(posY) {
startGps();
}

void startGps() {
// implementation of startGps function
}
};
In the above code, the constructor takes two parameters (posX and posY) and initializes the data members x and y using the initialization section (x(posX) and y(posY)). It also calls the member function startGps() without any parameters.

User Sanjiv
by
8.2k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.