Constructors question

I have a question about something in this lesson:

https://www.codecademy.com/courses/learn-c-plus-plus/lessons/cpp-classes-and-objects/exercises/cpp-constructors?action=resume_content_item

In the first picture, there is the code:

// city.hpp
#include "city.hpp"

class City {
    std::string name;
    int population;

public:
    City(std::string new_name, int new_pop);

};

//city.cpp
City::City(std::string new_name, int new_pop)
     // members get initialized to values passed in
     : name(new_name), population(new_pop) {}

I do not understand why we have #include “city.hpp” in the city.hpp section. Is this a typo? Shouldn’t the #include “city.hpp” be under the // city.cpp instead, like this?

//city.cpp
#include "city.hpp"
City::City(std::string new_name, int new_pop)
    // members get initialized to values passed in
    : name(new_name), population(new_pop) {}

This image has me confused. Also, wouldn’t the city.hpp need #include string at the top? Like this:

// city.hpp
#include <string>

class City {

    std::string name;
    int population;

public:
    City(std::string new_name, int new_pop);

};

Thanks.

1 Like