From 0adc2dad837d098b3df7bd1bc6665f0748185fd7 Mon Sep 17 00:00:00 2001 From: Nidu Rasnayake <106425621+Raptor2718@users.noreply.github.com> Date: Sun, 4 Feb 2024 15:01:07 +0000 Subject: [PATCH 1/3] Update app.cpp --- 7-classes-and-objects/app.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/7-classes-and-objects/app.cpp b/7-classes-and-objects/app.cpp index 229170e..de8df9d 100644 --- a/7-classes-and-objects/app.cpp +++ b/7-classes-and-objects/app.cpp @@ -1,13 +1,12 @@ -#include #include "profile.hpp" +#include +#include + 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 sam("Sam Drakkila", 30, "New York", "he/him"); -} \ No newline at end of file +sam.add_hobbies({"hiking", "reading"}); +std::cout << sam.view_profile() << "\n"; +} From 4a521a733d13fe7fa736f284a18090f5372974f5 Mon Sep 17 00:00:00 2001 From: Nidu Rasnayake <106425621+Raptor2718@users.noreply.github.com> Date: Sun, 4 Feb 2024 15:02:01 +0000 Subject: [PATCH 2/3] Update profile.cpp --- 7-classes-and-objects/profile.cpp | 49 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/7-classes-and-objects/profile.cpp b/7-classes-and-objects/profile.cpp index 4f40619..6dcd450 100644 --- a/7-classes-and-objects/profile.cpp +++ b/7-classes-and-objects/profile.cpp @@ -1,37 +1,34 @@ -#include - #include "profile.hpp" +#include +#include -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; - } +Profile::Profile (std::string name, unsigned int age, std::string city, std::string pronouns) + : name(name), age(age), city(city), pronouns(pronouns) {} +void Profile::add_hobbies(std::initializer_list new_hobbies) +{ + hobbies.insert(hobbies.end(), new_hobbies.begin(), new_hobbies.end()); } -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"; +std::string Profile::view_profile() +{ + std::string age_string = std::to_string(age); - for (std::string hobby : hobbies) { + std::string profile_info = "Name: " + Profile::name + "\n" + "Age: " + age_string + "\n" + "City: " + Profile::city + "\n" + "Pronouns: " + Profile::pronouns + "\n" + "Hobbies:"; - hobby_string += " - " + hobby + "\n"; + int size = hobbies.size(); + for (std::string i: hobbies) + { + if (i != hobbies[0]) + { + profile_info += ", " + i; + } + else + { + profile_info += " " + i; + } } - return bio + "\n" + hobby_string; - -} - -void Profile::add_hobby(std::string new_hobby) { - - hobbies.push_back(new_hobby); - + return profile_info; } From 9d9739c234c38157ccca674c58775d6232efc5ed Mon Sep 17 00:00:00 2001 From: Nidu Rasnayake <106425621+Raptor2718@users.noreply.github.com> Date: Sun, 4 Feb 2024 15:02:28 +0000 Subject: [PATCH 3/3] Update profile.hpp --- 7-classes-and-objects/profile.hpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/7-classes-and-objects/profile.hpp b/7-classes-and-objects/profile.hpp index 9a04429..437132f 100644 --- a/7-classes-and-objects/profile.hpp +++ b/7-classes-and-objects/profile.hpp @@ -1,17 +1,18 @@ #include +#include +#include class Profile { -private: + std::string name; - int age; + unsigned int age; std::string city; - std::string country; std::string pronouns; std::vector 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); -}; \ No newline at end of file + public: + Profile (std::string name, unsigned int age, std::string city, std::string pronouns); + + void add_hobbies (std::initializer_list new_hobbies); + std::string view_profile(); +};