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

时间:2014-01-14 15:16:40

标签: c++ object methods simplify

我想知道你是否在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;
};

正如你所看到的,他们都采用相同的论点,并且两者都需要相同的条件才能真正访问&#34; meat&#34;方法。

虽然我认为可能创建一个方法然后使用switch语句来访问方法的不同部分可能是个好主意,但我希望能够访问&#34; changeBalance&#34;部分或&#34; otherMethod&#34;课程的一部分。我只是不确定是否有办法让这一点变得更加简洁,并简化它?

我的整个代码是:

#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 Balance\n 2. Deposit\n 3. Change pin\nChoice: ";
        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;
        } 
    }
}

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

谢谢。

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

您可以将“受限制”接口封装在另一种类型中,并且只允许使用有效凭据创建该类型。像

这样的东西
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本身的构造函数中。

答案 2 :(得分:1)

您可以考虑将此作为交易。

(伪代码/设计)

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