It is my understanding that we add #include string to the top of the program when the data type std::string is involved. However, something has me confused. In the answer key for the FreeForm project “The Object of Your Affection”, the data type std::string is involved in profile.hpp but there is no #include string at the top.
Here is the link to the lesson
https://www.codecademy.com/courses/learn-c-plus-plus/projects/cpp-dating-profile
Here is the answer key for the lesson
Here is the code for those who do not want to click the links
app.cpp
#include <iostream>
#include "profile.hpp"
int main() {
Profile sam("Sam Drakkila", 30, "New York", "USA", "he/him");
sam.add_hobby("listening to audiobooks and podcasts");
sam.add_hobby("playing rec sports like bowling and kickball");
sam.add_hobby("writing a speculative fiction novel");
sam.add_hobby("reading advice columns");
std::cout << sam.view_profile();
}
profile.hpp
#include <vector>
class Profile {
private:
std::string name;
int age;
std::string city;
std::string country;
std::string pronouns;
std::vector<std::string> hobbies;
public:
Profile(std::string new_name, int new_age, std::string new_city, std::string new_country, std::string new_pronouns = "they/them");
std::string view_profile();
void add_hobby(std::string new_hobby);
};
profile.cpp
#include <iostream>
#include "profile.hpp"
Profile::Profile(std::string new_name, int new_age, std::string new_city, std::string new_country, std::string new_pronouns)
: name(new_name), age(new_age), city(new_city), country(new_country), pronouns(new_pronouns) {
if (new_age >= 18) {
age = new_age;
} else {
age = 0;
}
}
std::string Profile::view_profile() {
std::string bio = "Name: " + name;
bio += "\nAge: " + std::to_string(age);
bio += "\nPronouns: " + pronouns;
std::string hobby_string = "Hobbies:\n";
for (std::string hobby : hobbies) {
hobby_string += " - " + hobby + "\n";
}
return bio + "\n" + hobby_string;
}
void Profile::add_hobby(std::string new_hobby) {
hobbies.push_back(new_hobby);
}
My question is, shouldn’t profile.hpp need #include string at the top? I have run the code and it runs perfectly fine without it. Why is this the case? When is #include string absolutely necessary?