Skip to content
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

classes and objects OOYA project #85

Open
wants to merge 3 commits 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: 7 additions & 8 deletions 7-classes-and-objects/app.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include <iostream>
#include "profile.hpp"
#include <iostream>
#include <string>


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

}
sam.add_hobbies({"hiking", "reading"});
std::cout << sam.view_profile() << "\n";
}
49 changes: 23 additions & 26 deletions 7-classes-and-objects/profile.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
#include <iostream>

#include "profile.hpp"
#include <iostream>
#include <string>

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<std::string> 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;
}
19 changes: 10 additions & 9 deletions 7-classes-and-objects/profile.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#include <vector>
#include <iostream>
#include <string>

class Profile {
private:
std::string name;
int age;
unsigned 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);

};
public:
Profile (std::string name, unsigned int age, std::string city, std::string pronouns);

void add_hobbies (std::initializer_list<std::string> new_hobbies);
std::string view_profile();
};