Skip to content

Instantly share code, notes, and snippets.

@drodil
Last active March 8, 2019 07:43
Show Gist options
  • Save drodil/8824726de0785f78f25f19d609de3309 to your computer and use it in GitHub Desktop.
Save drodil/8824726de0785f78f25f19d609de3309 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdexcept>
class Note {
public:
int PublicField;
std::string GetPrivateField() {
return privateField;
}
void SetPrivateField(const std::string& field) {
ensurePrivateFieldLength(field);
privateField = field;
}
private:
void ensurePrivateFieldLength(const std::string& field) {
if(field.length() == 0) {
throw new std::invalid_argument("Empty private field given!");
}
}
std::string privateField;
};
static Note NewNote(const std::string& field) {
Note inst{};
inst.PublicField = 2;
inst.SetPrivateField(field);
return inst;
}
int main(int argc, char** argv) {
std::cout << "You have given " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
}
try {
auto note = NewNote("my note");
std::cout << note.PublicField << std::endl;
std::cout << note.GetPrivateField() << std::endl;
Note* notepointer;
notepointer = &note;
std::cout << notepointer->PublicField << std::endl;
std::cout << notepointer->GetPrivateField() << std::endl;
} catch(std::invalid_argument& ex) {
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment