Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class LoginID // Start classname with a capital.
- {
- private:
- /* Since these variables are private you need methods like "getPassword()"
- to retrieve their content. It might be simpler if you made these public then
- you could acces them as: LoginID loginID; loginID.password = "123";
- If you want to filter user input (safer) then its better to keep these private
- and use properties (get and set functions) like "setPassword()" to check the
- users input (For example check if a password is strong enough);
- Its up to you to decide wich one is suited. */
- string password;
- string username;
- int maxTries;
- public:
- LoginID(string un, string pw)
- {
- // This is a constructor, it does not have a return value (No void, no string, ...)
- // and it has the same name as the class.
- // We initialise the class like this:
- password = pw;
- username = un;
- maxTries = 3; // Use setMaxTries() to change
- // Or like this! :
- // this->password = pw;
- // this->username = un;
- /* The 'this' keyword is actually an argument that is passed along "un" and "pw"
- implicitly, you don't see it happening, you don't pass it yourself but it IS there.
- "this" is a pointer to the class from which this function is called, you can use it to
- set the members of THIS object. Look up the "->" operator for C++
- In the python language this is more clear, you define a class with a class-method like this:
- class test (object): # This is a class
- def __init__(self, un, pw): # This is a constructor, note 'self', its the equivalent of 'this'
- self.password = pw # As you can see in python we DO write this keywoard explicitly, but works the same
- self.username = un
- myTest = test("Jonas", "1234") # We only have to use 2 arguments, even though the __init__() function takes 3,
- # the first parameter 'self' is passed by python's internals, it points to 'myTest'
- # This statement is the same as: myTest.username = "Jonas"
- myTest.password = "1234" but much shorter!
- */
- }
- void setPassword(string password)
- {
- this->password = password; // We prefer to use 'this->password' to differentiate between the argument 'password'
- // that is passed to the method setPassword(string password) and the class member 'password'
- } // ^
- void setUsername(string username)
- {
- this->username = username;
- }
- void setMaxTries(int max)
- {
- maxTries = max;
- }
- string getPassword()
- {
- return password;
- }
- string getUsername()
- {
- return username;
- }
- int getMaxtries()
- {
- return maxTries;
- }
- };
- int main()
- {
- string passwordCreate;
- string passwordUse;
- string usernameCreate;
- string usernameUse;
- int triess = 0;
- int maxAttempts;
- cout << "Type in a username you want to use: " << endl;
- getline (cin, usernameCreate);
- cout << "Please enter a password you want to use: " << endl;
- getline (cin, passwordCreate);
- cout << "As a security precatuion set the max amount of tries you sholud get before being unable to login: " << endl;
- cin >> maxAttempts;
- LoginID loginID (usernameCreate, passwordCreate); // Classname has a capital, class object has no capital (Convention for readability)
- loginID.setMaxTries(maxAttempts);
- cout << "Now is the time to test your new account. press ENTER to continiue." << endl;
- cin.get();
- cin.ignore();
- do // First ask username, then ask again if its wrong, thats the idea behind a do-while loop
- {
- cout << "Username: " << endl;
- getline (cin, usernameUse);
- if (usernameUse != loginID.getUsername())
- cout << "Invalid username, try again." << endl;
- }
- while (usernameUse != loginID.getUsername());
- do // Add max tries functionality yourself
- {
- cout << "Password: " << endl;
- getline (cin, passwordUse);
- if (passwordUse != loginID.getPassword())
- cout <<"The password entered is incorrect, please try again." << endl;
- }
- while (passwordUse != loginID.getPassword());
- cout << "The password is correct." << endl << endl;
- cout << "You are now godlike." << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment