Guest User

Untitled

a guest
Oct 6th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class LoginID // Start classname with a capital.
  6. {
  7. private:
  8. /* Since these variables are private you need methods like "getPassword()"
  9. to retrieve their content. It might be simpler if you made these public then
  10. you could acces them as: LoginID loginID; loginID.password = "123";
  11. If you want to filter user input (safer) then its better to keep these private
  12. and use properties (get and set functions) like "setPassword()" to check the
  13. users input (For example check if a password is strong enough);
  14. Its up to you to decide wich one is suited. */
  15.  
  16. string password;
  17. string username;
  18. int maxTries;
  19.  
  20. public:
  21. LoginID(string un, string pw)
  22. {
  23. // This is a constructor, it does not have a return value (No void, no string, ...)
  24. // and it has the same name as the class.
  25.  
  26. // We initialise the class like this:
  27. password = pw;
  28. username = un;
  29. maxTries = 3; // Use setMaxTries() to change
  30. // Or like this! :
  31.  
  32. // this->password = pw;
  33. // this->username = un;
  34.  
  35. /* The 'this' keyword is actually an argument that is passed along "un" and "pw"
  36. implicitly, you don't see it happening, you don't pass it yourself but it IS there.
  37. "this" is a pointer to the class from which this function is called, you can use it to
  38. set the members of THIS object. Look up the "->" operator for C++
  39.  
  40. In the python language this is more clear, you define a class with a class-method like this:
  41. class test (object): # This is a class
  42. def __init__(self, un, pw): # This is a constructor, note 'self', its the equivalent of 'this'
  43. self.password = pw # As you can see in python we DO write this keywoard explicitly, but works the same
  44. self.username = un
  45.  
  46. myTest = test("Jonas", "1234") # We only have to use 2 arguments, even though the __init__() function takes 3,
  47. # the first parameter 'self' is passed by python's internals, it points to 'myTest'
  48. # This statement is the same as: myTest.username = "Jonas"
  49. myTest.password = "1234" but much shorter!
  50. */
  51. }
  52.  
  53. void setPassword(string password)
  54. {
  55. this->password = password; // We prefer to use 'this->password' to differentiate between the argument 'password'
  56. // that is passed to the method setPassword(string password) and the class member 'password'
  57. } // ^
  58.  
  59. void setUsername(string username)
  60. {
  61. this->username = username;
  62. }
  63.  
  64. void setMaxTries(int max)
  65. {
  66. maxTries = max;
  67. }
  68.  
  69. string getPassword()
  70. {
  71. return password;
  72. }
  73.  
  74. string getUsername()
  75. {
  76. return username;
  77. }
  78.  
  79. int getMaxtries()
  80. {
  81. return maxTries;
  82. }
  83. };
  84.  
  85. int main()
  86. {
  87. string passwordCreate;
  88. string passwordUse;
  89. string usernameCreate;
  90. string usernameUse;
  91. int triess = 0;
  92. int maxAttempts;
  93.  
  94. cout << "Type in a username you want to use: " << endl;
  95. getline (cin, usernameCreate);
  96. cout << "Please enter a password you want to use: " << endl;
  97. getline (cin, passwordCreate);
  98. cout << "As a security precatuion set the max amount of tries you sholud get before being unable to login: " << endl;
  99. cin >> maxAttempts;
  100.  
  101. LoginID loginID (usernameCreate, passwordCreate); // Classname has a capital, class object has no capital (Convention for readability)
  102. loginID.setMaxTries(maxAttempts);
  103.  
  104. cout << "Now is the time to test your new account. press ENTER to continiue." << endl;
  105. cin.get();
  106. cin.ignore();
  107.  
  108. do // First ask username, then ask again if its wrong, thats the idea behind a do-while loop
  109. {
  110. cout << "Username: " << endl;
  111. getline (cin, usernameUse);
  112.  
  113. if (usernameUse != loginID.getUsername())
  114. cout << "Invalid username, try again." << endl;
  115. }
  116. while (usernameUse != loginID.getUsername());
  117.  
  118. do // Add max tries functionality yourself
  119. {
  120. cout << "Password: " << endl;
  121. getline (cin, passwordUse);
  122.  
  123. if (passwordUse != loginID.getPassword())
  124. cout <<"The password entered is incorrect, please try again." << endl;
  125. }
  126. while (passwordUse != loginID.getPassword());
  127.  
  128. cout << "The password is correct." << endl << endl;
  129. cout << "You are now godlike." << endl;
  130.  
  131. return 0;
  132. }
Add Comment
Please, Sign In to add comment