类成员中的相同参数:简化
Same arguments in members of a class: 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;
};
如您所见,它们都采用相同的参数,并且都需要相同的条件才能访问该方法的"肉"。
虽然我认为可能创建一个方法,然后有一个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
,其中包含作为私有成员的acct
和pin
,以及一个将检查相等性的成员函数。
您可以将
"受限"接口封装在另一种类型中,并且仅允许使用有效凭据创建该类型。类似的东西
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();
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 将强制转换简化为取决于参数的类型
- 使用全局 std::array 引用作为模板参数时如何简化参数?
- 如何简化模板模板参数中的enable_if别名
- 简化可变参数模板:删除一些专用项
- 简化模板参数
- 类成员中的相同参数:简化
- 简化模板参数
- 如何使用许多模板参数简化表示法,并在C++的另一个模板类中使用它
- 如何使用typedef来简化作为函数指针的模板函数参数
- 如何将可变模板参数的特征值简化为一个值
- 模板参数简化
- 使模板参数公开的简化方法
- 编译器是否会将给出常量参数的简单函数简化为唯一的指令?
- 将函数作为参数传递以简化线程创建
- 指向具有简化参数的函数的指针