Game Development Reference
In-Depth Information
Listing 10-1. The Vehicle Class
class Vehicle
{
private:
unsigned int m_numberOfWheels;
public:
Vehicle();
unsigned int GetNumberOfWheels() const;
};
Vehicle::Vehicle()
: m_numberOfWheels(0)
{
}
unsigned int Vehicle::GetNumberOfWheels() const
{
return m_numberOfWheels;
}
The Vehicle class contains a private variable that stores the number of wheels the vehicle has.
The variable is private and the Vehicle class does not provide a method to set it. Instead, classes
that derive from Vehicle will set their own number of wheels in their own constructor.
Note The constructor for the Vehicle class uses an initializer list to initialize its variables. An initializer list
ensures that the variables' values are set before any of the code contained within the constructor is executed
and is the preferred method for initializing class member variables. You can use an initializer list in your class
constructors by following the constructor signature with a colon and placing the value to initialize within
brackets. Subsequent variables can be added to the list using commas.
Listing 10-2 shows the class definition for a Car .
Listing 10-2. The Car Class
class Car : public Vehicle
{
public:
Car();
};
Car::Car()
{
m_numberOfWheels = 4;
}
 
Search WWH ::




Custom Search