分享代码,仅供参考
#ifndef AC #define AC #include<iostream> #include<cmath> #include<cstring> #include<string> using namespace std; class User { public: User(string N, string P="111111"); void showdata(); void setpassword(); void static showlastID(User *p); private: int id; string name; string password; static int CurrentID; }; User *p=NULL; int User::CurrentID=999; #endif
#include"User.h" User::User(string N, string P): name(N), password(P) { id=++CurrentID; p=this; } void User::showdata() { cout << "编号: " << id << endl << "姓名: " << name << endl << "密码: " << password << endl; } void User::setpassword() { cout << "请输入初始密码" << endl; string s; int count=3; bool isTrue=true; cin >> s; if (s!=password) { isTrue=false; while (!isTrue && count >0) { cout << "输入错误,你还有" << count-- << "次机会" << endl; cin >> s; if (s==password) { isTrue=true; break; } }
if (!isTrue && !count) cout << "抱歉,请稍后再试" << endl; } if (isTrue==true) { cout << "请输入新的密码" << endl; cin >> s; password=s; } } void User::showlastID(User *p) { cout << CurrentID << endl; p->showdata(); }
#include"User.cpp" int main() { User a("bxy"); a.setpassword(); a.showdata();
cout << "----------------------" << endl; User b("tlz", "0516"); b.setpassword(); b.showdata();
cout << "----------------------" << endl; User::showlastID(p); return 0; }