类成员中的相同参数:简化

Same arguments in members of a class: Simplify?

本文关键字:参数 简化 成员      更新时间:2023-10-16

我想知道您是否有具有相同参数的类C++的成员,是否有办法简化这一点?例如:

#include <iostream>
using namespace std:
class bankAccount
{
public:
        bankAccount()
        {
           privAcct = "MyAccount";
           privPin = "MyPin";
        {
        void changeBalance(string acct, string pin)
        {
           if(acct == privAcct && pin == privPin)
           {
               cout << "YAY! You can do this!" << endl;
           }
        }
        void otherMethod(string acct, string pin)
        {
           if(acct == privAcct && pin == privPin)
           {
               cout << "YAY! You can do this!" << endl;
           }
        }
private:
        string privAcct, privPin;
};

如您所见,它们都采用相同的参数,并且都需要相同的条件才能访问该方法的"肉"。

虽然我认为可能创建一个方法,然后有一个switch语句来访问方法的不同部分可能是个好主意,但我希望能够访问类的"changeBalance"部分或"otherMethod"部分。我只是不确定是否有办法让它更"整洁"一点,或者简化它?

我的整个代码是这样的:

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void accountInfo();
class account{
public:
    //constructor
    account()
    {
        balance = 0.00;
        accountNum = "0303";
        pin = "2222";
    }
    //default for wrong entry response
    void wrongEntry()
    {
        cout << "Wrong account number or PIN. Please try again.n" << endl;
    }
    //change pin number
    void changePin(string actNum, string oldPin)
    {
        if(actNum == accountNum && oldPin == pin)
        {
            string newPin;
            cout << "Please enter in a new pin: ";
            cin >> newPin;
            pin = newPin;
            cout << "Thank you." << endl;
        }
        else
        {
            wrongEntry();
        }
    }
    //change balance
    void changeBalance(string actNum, string pinnum)
    {
        if(actNum == accountNum && pinnum == pin)
        {
            double newAdd;
            cout << "Your current balance is " << balance << "nPlease enter the additional amount you would like to deposit: ";
            cin >> newAdd;
            balance += newAdd;
            cout << "Your new balance is " << balance << ".nThank you.n" << endl;
        }
        else
        {
            wrongEntry();
        }
    }
    //print balance and account #
    void printBalance(string actNum, string pinnum)
    {
        if(actNum == accountNum && pinnum == pin)
        {
            cout << "For account #"<< accountNum << endl;
            cout << "Your current balance is $" << balance << ".nThank you.n";
        }
        else
        {
            wrongEntry();
        }
    }
private:
    string accountNum, pin;
    double balance;
};
int main ()
{
    int input;
    string aN, pin;
    account bo;
    while(1) 
    {
        cout << "Please enter account number: ";
        cin >> aN;
        cout << "Please enter corresponding PIN: ";
        cin >> pin;
        ///options
        cout << "Please choose from the following options:n";
        cout << " 1. View Balancen 2. Depositn 3. Change pinnChoice: ";
        cin >> input;
        cout << endl;
        switch(input)
        {
        case 1:
            bo.printBalance(aN, pin);
            break;
        case 2:
            bo.changeBalance(aN, pin);
            break;
        case 3:
            bo.changePin(aN, pin);
            break;
        default:
            cout << "The information you entered does not seem to match our records.nPlease try again.n";
            break;
        }
        char response;
        cout << "nAre you done with your transaction?: Y / N";
        cin >> response;
        cout << endl;
        if(response == 'Y' || response == 'y')
        {
            return 0;
        } 
    }
}

意识到我应该将我的类放在单独的头文件中,同时正确声明构造函数,但我只是想简化代码。

谢谢。

您可以创建一个新的类LoginCredentials,其中包含作为私有成员的acctpin,以及一个将检查相等性的成员函数。

您可以将

"受限"接口封装在另一种类型中,并且仅允许使用有效凭据创建该类型。类似的东西

class Account {
public:
    struct Accessor {
        Accessor(Account & a, string acct, string pin) : account(&a) {
            if (acct != a.privAcct || pin != a.privPin) {
                throw BadCredentials();
            }
        }
        void changePin() {account->changePin();}
        // and other functions
        Account * account;
    };
private:
    string privAcct, privPin;
    // Only accessible through an Accessor
    void changePin();
    // and other functions
};

用法:

Accessor(account, acct, pin).changePin();

或者,如果整个接口受到限制,则可以简单地将验证放在Account本身的构造函数中。

您可以将其视为事务。

(伪代码/设计)

transactionValid  = BeginTransation(account, pin);
if ( transactionValid )
{
  changePin(...);
  chackBalance(...);
}
EndTransaction();