Skip to content

Instantly share code, notes, and snippets.

@michaelfm1211
Last active July 4, 2022 04:30
Show Gist options
  • Save michaelfm1211/b799beb8b49f74973e2603c420d7cd8b to your computer and use it in GitHub Desktop.
Save michaelfm1211/b799beb8b49f74973e2603c420d7cd8b to your computer and use it in GitHub Desktop.
like my idiomatic oop & c++ skills?
#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