求C++中usernameExist函数实现帮助:基于Account类的账户列表查重
Hey there! Let's work through this problem step by step. First, there's a small hurdle we need to clear before implementing the usernameExist function: your Account class has the username member marked as private, which means external functions like usernameExist can't directly access it. We have two common ways to fix this:
Option 1: Add a Public Getter Method (Recommended)
This follows encapsulation best practices by only exposing the data we need, rather than opening up full access to private members. First, update your Account class with a constructor (since you're instantiating objects with parameters) and a getter for the username:
#include <string> using namespace std; // Or qualify with std:: if you prefer class Account { private: string name; unsigned int age; string username; string password; public: // Constructor to initialize account values Account(string n, unsigned int a, string un, string p) : name(n), age(a), username(un), password(p) {} // Getter to access the private username string getUsername() const { return username; } };
Now we can implement the usernameExist function safely:
bool usernameExist(Account* userAccount, Account* accountList[], int numberOfElements) { // Grab the target username from the provided account const string targetUsername = userAccount->getUsername(); // Iterate through every account in the list for (int i = 0; i < numberOfElements; ++i) { // Skip null pointers to avoid crashes if (accountList[i] != nullptr) { if (accountList[i]->getUsername() == targetUsername) { return true; // Match found! } } } return false; // No matching username found }
Option 2: Use a Friend Function
If you prefer not to add a getter, you can declare usernameExist as a friend of the Account class to grant it direct access to private members:
#include <string> using namespace std; class Account { private: string name; unsigned int age; string username; string password; public: Account(string n, unsigned int a, string un, string p) : name(n), age(a), username(un), password(p) {} // Declare the function as a friend to access private members friend bool usernameExist(Account* userAccount, Account* accountList[], int numberOfElements); }; // Implement the friend function bool usernameExist(Account* userAccount, Account* accountList[], int numberOfElements) { const string targetUsername = userAccount->username; for (int i = 0; i < numberOfElements; ++i) { if (accountList[i] != nullptr) { if (accountList[i]->username == targetUsername) { return true; } } } return false; }
How to Use the Function
Here's a quick example of how to call usernameExist in your main program:
int main() { // Create your account instance Account first("Max", 18,"maxx34","12345adc"); Account second("Luna", 20,"luna_99","moonlight123"); // Create an array of Account pointers Account* accountList[] = {&first, &second}; int numAccounts = sizeof(accountList) / sizeof(accountList[0]); // Check if Max's username exists in the list bool exists = usernameExist(&first, accountList, numAccounts); // `exists` will be true here }
内容的提问来源于stack exchange,提问作者Gre3n

