C/C++ Arena

Step 3 of 9

Constructors

A constructor is a special member function that runs when an object is created. It has the class's name and no return type. Use a member initializer list (after :) to set members:

class Player {
public:
    Player(std::string name, int hp) : name_(name), hp_(hp) {}
private:
    std::string name_;
    int hp_;
};

Player p("zywoo", 100);

Your turn: complete the constructor's initializer list.

Previous: public and private Next: const member functions