Last active
March 8, 2019 07:43
-
-
Save drodil/8824726de0785f78f25f19d609de3309 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
#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 = ¬e; | |
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