Skip to content

The Object Of Your Affection #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions 7-classes-and-objects/app.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#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 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;
}
57 changes: 29 additions & 28 deletions 7-classes-and-objects/profile.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
#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) {
{
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);
}
24 changes: 12 additions & 12 deletions 7-classes-and-objects/profile.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include <vector>

class Profile {
class Profile
{
private:
std::string name;
int age;
std::string city;
std::string country;
std::string pronouns;
std::vector<std::string> hobbies;

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(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);

};
}; #pragma once