From 650149e6af762d1aadf658afb9565007f0f60fd5 Mon Sep 17 00:00:00 2001 From: sbhatia57 Date: Sat, 5 Apr 2025 11:50:16 -0400 Subject: [PATCH] The Object Of Your Affection I completed the project. --- 7-classes-and-objects/app.cpp | 15 ++++---- 7-classes-and-objects/profile.cpp | 57 ++++++++++++++++--------------- 7-classes-and-objects/profile.hpp | 24 ++++++------- 3 files changed, 47 insertions(+), 49 deletions(-) diff --git a/7-classes-and-objects/app.cpp b/7-classes-and-objects/app.cpp index 229170e..e1d7ddf 100644 --- a/7-classes-and-objects/app.cpp +++ b/7-classes-and-objects/app.cpp @@ -1,13 +1,10 @@ #include #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 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() << std::endl; } \ No newline at end of file diff --git a/7-classes-and-objects/profile.cpp b/7-classes-and-objects/profile.cpp index 4f40619..3bbc932 100644 --- a/7-classes-and-objects/profile.cpp +++ b/7-classes-and-objects/profile.cpp @@ -1,37 +1,38 @@ #include - #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) { +{ + name = new_name; age = new_age; - } else { - age = 0; - } - + city = new_city; + country = new_country; + pronouns = new_pronouns; } -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; - +std::string Profile::view_profile() +{ + std::string profileInfo = "Name: " + name + "\n"; + profileInfo += "age: " + std::to_string(age); + profileInfo += "\ncity: " + city; + profileInfo += "\ncountry: " + country; + profileInfo += "\npronouns: " + pronouns; + profileInfo += "\nhobbies: "; + + // Loop through users's hobbies included in their profile + for (int hobbyIndex = 0; hobbyIndex < hobbies.size(); hobbyIndex++) + { + // Deals with edge case of the last item + if ((hobbyIndex + 1) < hobbies.size()) + profileInfo += hobbies[hobbyIndex] + ", "; + else + profileInfo += hobbies[hobbyIndex]; + } + + return profileInfo; } -void Profile::add_hobby(std::string new_hobby) { - - hobbies.push_back(new_hobby); - -} +void Profile::add_hobby(std::string new_hobby) +{ + hobbies.push_back(new_hobby); +} \ No newline at end of file diff --git a/7-classes-and-objects/profile.hpp b/7-classes-and-objects/profile.hpp index 9a04429..01cda8f 100644 --- a/7-classes-and-objects/profile.hpp +++ b/7-classes-and-objects/profile.hpp @@ -1,17 +1,17 @@ #include -class Profile { +class Profile +{ private: - std::string name; - int age; - std::string city; - std::string country; - std::string pronouns; - std::vector hobbies; - + std::string name; + 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); + 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 +}; #pragma once