Last active
July 4, 2022 04:30
-
-
Save michaelfm1211/b799beb8b49f74973e2603c420d7cd8b to your computer and use it in GitHub Desktop.
like my idiomatic oop & c++ skills?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class HelloSayer { | |
private: | |
std::string greeting; | |
std::string name; | |
std::string makeMessage() { | |
return this->greeting + ", " + this->name + "!"; | |
} | |
public: | |
HelloSayer(std::string greeting, std::string name) { | |
this->greeting = greeting; | |
this->name = name; | |
} | |
void printHello() { | |
std::cout << makeMessage() << std::endl; | |
} | |
}; | |
class HelloSayerFactory { | |
private: | |
std::string greeting; | |
public: | |
HelloSayerFactory(std::string greeting) { | |
this->greeting = greeting; | |
} | |
HelloSayer *makeSayer(std::string name) { | |
return new HelloSayer(this->greeting, name); | |
} | |
}; | |
int main(int argc, char *argv[]) { | |
if (argc < 2) { | |
std::cerr << "no name given" << std::endl; | |
return 1; | |
} | |
HelloSayerFactory hsf("Hello"); | |
std::string username(argv[1]); | |
HelloSayer *user = hsf.makeSayer(username); | |
user->printHello(); | |
delete user; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment