Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Constructors

Every hero made from the class so far is Aria with 100 health, because those values are written in the class. A game needs heroes with different names and health, but the members are private, so main can't fill them in. The class needs a way to get its starting values: a constructor.

#include <iostream>
#include <string>

class Hero {
private:
    std::string name;
    int health;

public:
    Hero(std::string heroName, int startHealth) {
        name = heroName;
        health = startHealth;
    }

    void printStatus() const {
        std::cout << name << ": health " << health << std::endl;
    }
};

int main() {
    Hero aria("Aria", 100);
    Hero finn("Finn", 80);
    aria.printStatus();
    finn.printStatus();
    return 0;
}

This prints:

Aria: health 100
Finn: health 80

A constructor is a method with the name of the class and without a return type, not even void. It runs by itself, once, whenever an object is created. Hero aria("Aria", 100); creates the object aria and passes "Aria" and 100 to the constructor, which stores them in the members. You can also write Hero aria = Hero("Aria", 100); or Hero aria = {"Aria", 100};: with a constructor, braces work again, because they call it. This course uses the first form.

The parameters have names of their own, heroName and startHealth. If a parameter were called name, then inside the constructor, name would mean the parameter and not the member, and name = name; would store the parameter in itself. The member would stay empty, and the program would print heroes without names.

Once a class has a constructor, every object has to be created through it. If you add the line Hero hero; below Finn in the program above, compiling ends with:

main.cpp:23:10: error: no matching constructor for initialization of 'Hero'

The notes below the error list the constructors that could have been used, and why none of them fits. The last one is yours: candidate constructor not viable: requires 2 arguments, but 0 were provided. It needs two values and got none. That's good news: a hero without a name and health can't be created by mistake.

Instructions

  1. 1

    Replace the default values of the members with a constructor Hero(std::string heroName, int startHealth), which stores its parameters in the members. In main, create Aria through it, with the name Aria and 100 health. The program prints Aria: health 100, as before.

  2. 2

    Below Aria, create a second hero finn with the name Finn and 80 health. Finn takes 25 damage. Then print his status too. The program prints Aria: health 100 and then Finn: health 55.

Back

5/11

  1. 1. Methods
  2. 2. Methods That Change the Object
  3. 3. Private and Public Members
  4. 4. Getters and const Methods
  5. 5. Constructors
  6. 6. Quiz: Constructors
  7. 7. Rules in One Place
  8. 8. Objects as Parameters
  9. 9. A Vector of Objects
  10. 10. Quiz: Damage in a Loop
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.