47 lines
931 B
C++
47 lines
931 B
C++
// skeleton.cpp
|
|
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <memory>
|
|
|
|
class Person
|
|
{
|
|
std::string name;
|
|
std::map<std::string, std::string> attributes {};
|
|
|
|
public:
|
|
|
|
Person(std::string name)
|
|
{
|
|
this->name = name;
|
|
}
|
|
|
|
Person* addAttribute(std::string key, std::string value)
|
|
{
|
|
this->attributes[key] = value;
|
|
return this;
|
|
}
|
|
|
|
void greet()
|
|
{
|
|
std::cout << "Hello, my name is " << this->name << "!" << std::endl;
|
|
for (auto& kv : this->attributes) {
|
|
std::cout << " My " << kv.first << " is " << kv.second << "." << std::endl;
|
|
}
|
|
}
|
|
};
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
auto person = std::unique_ptr<Person>((new Person("Madison"))
|
|
->addAttribute("best friend", "Bob")
|
|
->addAttribute("favorite color", "pink")
|
|
);
|
|
|
|
person->greet();
|
|
// Not necessary with unique_ptr:
|
|
// delete person;
|
|
|
|
return 0;
|
|
}
|