4.30 编写一个程序,已有若干用户的用户名和密码,密码输入时不能显示输入结果,记录用户个数,最后将信息输出。(用静态数据成员表示用户的个数)。
#include<iostream>
#include<vector>
#include<conio.h>
using namespace std;
class User {
private:
string username;
string password;
static int userCount;
public:
User(string uName, string pwd) {
username = uName;
password = pwd;
userCount++;
}
void display() {
cout << "Username: " << username << endl;
cout << "Password: ********" << endl;
}
static int countUsers() {
return userCount;
}
};
int User::userCount = 0;
int main() {
vector<User> users;
char choice = 'y';
string uName, pwd;
while (choice == 'y' || choice == 'Y') {
cout << "Enter username: ";
cin >> uName;
cout << "Enter password: ";
char c = _getch();
pwd = "";
while (c != 13) {
pwd.push_back(c);
cout << "*";
c = _getch();
}
cout << endl;
users.push_back(User(uName, pwd));
cout << "Do you want to add more users?(y/n): ";
cin >> choice;
}
cout << "\n\nUser Details: \n" << endl;
for (int i = 0; i < users.size(); i++) {
cout << "User " << i + 1 << ": " << endl;
users[i].display();
cout << endl;
}
cout << "\nTotal number of users: " << User::countUsers() << endl;
return 0;
}