gpt4 book ai didi

c++ - 类 : Simplify? 成员中的相同参数

转载 作者:太空宇宙 更新时间:2023-11-04 16:17:28 25 4
gpt4 key购买 nike

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

我意识到我应该将我的类放在单独的头文件中,并适本地声明一个构造函数,但我只是想简化代码。

谢谢。

最佳答案

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

关于c++ - 类 : Simplify? 成员中的相同参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21117058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com