思路:一个类,有变量用户名,密码,和验证码,有一些函数,读取用户名密码 验证验证码
验证码发送可以是虚拟的,语句提示即可,程序通过也可以是虚拟的,语句提示即可
思路:一个类,有变量用户名,密码,和验证码,有一些函数,读取用户名密码 验证验证码
验证码发送可以是虚拟的,语句提示即可,程序通过也可以是虚拟的,语句提示即可
friend
三个函数的逻辑放到main
里面,不作为函数处理(看第二个代码)
#include <iostream>
#include <cstring>
using namespace std;
class User
{
public:
User(string un, string pw, int vs) : username(un), password(pw), verifysum(vs) {}
~User() {cout << "Exit~" << endl;}
string GetUserName();
string GetPassWord();
int GetVerifySum();
void SetPassword(string pw);
friend bool LoginByPassWord(User& u, string un, string pw);
friend bool LoginByVerifySum(User& u, string un, int vs);
friend void ChangePassWord(User& u, string pw);
private:
string username;
string password;
int verifysum;
};
string User::GetUserName()
{
return username;
}
string User::GetPassWord()
{
return password;
}
int User::GetVerifySum()
{
return verifysum;
}
void User::SetPassword(string pw)
{
password = pw;
}
bool LoginByPassWord(User& u, string un, string pw)
{
if (u.GetUserName() == un && u.GetPassWord() == pw)
{
cout << "Login success." << endl;
return true;
}
return false;
}
bool LoginByVerifySum(User& u, string un, int vs)
{
if (u.GetUserName() == un && u.GetVerifySum() == vs)
{
cout << "Login success." << endl;
return true;
}
return false;
}
void ChangePassWord(User& u, string pw)
{
u.SetPassword(pw);
cout << "Change password success." << endl;
}
int main()
{
User u("admin", "admin", 520); //初始化对象
int first = 4;
string n_un, n_pw;
int n_vs;
while (first--)
{
cout << "Please input username and paddword: ";
cin >> n_un >> n_pw;
if (LoginByPassWord(u, n_un, n_pw))
{
return 0;
}
cout << "Login failed, remaining times: " << first << endl;
}
cout << "Please use the verification code to verify the login." << endl;
cout << "Please input verifysum: ";
cin >> n_vs;
if (LoginByVerifySum(u, n_un, n_vs))
{
cout << "Please change your password: ";
cin >> n_pw;
ChangePassWord(u, n_pw);
}
else
{
cout << "VerifySum is Error." << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
class User
{
public:
User(string un, string pw, int vs) : username(un), password(pw), verifysum(vs) {}
~User() {cout << "Exit~" << endl;}
string GetUserName();
string GetPassWord();
int GetVerifySum();
void SetPassword(string pw);
private:
string username;
string password;
int verifysum;
};
string User::GetUserName()
{
return username;
}
string User::GetPassWord()
{
return password;
}
int User::GetVerifySum()
{
return verifysum;
}
void User::SetPassword(string pw)
{
password = pw;
}
int main()
{
User u("admin", "admin", 520); //初始化对象
int first = 4;
string n_un, n_pw;
int n_vs;
while (first--)
{
cout << "Please input username and paddword: ";
cin >> n_un >> n_pw;
if (u.GetUserName() == n_un && u.GetPassWord() == n_pw)
{
cout << "Login success." << endl;
return 0;
}
cout << "Login failed, remaining times: " << first << endl;
}
cout << "Please use the verification code to verify the login." << endl;
cout << "Please input verifysum: ";
cin >> n_vs;
if (u.GetUserName() == n_un && u.GetVerifySum() == n_vs)
{
cout << "Login success." << endl;
cout << "Please change your password: ";
cin >> n_pw;
u.SetPassword(n_pw);
}
else
{
cout << "VerifySum is Error." << endl;
}
return 0;
}