如何将其他数据成员"number"添加到帐户类?
How to add an additional data member "number" to Account class?
我想我对语法有点困惑。我需要帮助添加一个名为number的额外数据成员到类帐户。我该怎么做??我尝试添加一个名为number的附加数据成员,它将代表一个唯一的帐号。数据生成器将用于定位帐户,因此需要一个构造函数/或编辑构造函数和"显示"方法。
这是账户代码。h:
#define START_BALANCE 500.0
#define START_INTEREST_RATE 0.0175
// define Account class
class Account
{
private:
double balance;
doube interestRate;
public:
// constructor with default values
// set to default values if value(s) is/are not valid
Account(double initialBalance, double initialInterestRate);
// observers
double getBalance() const; // return balance
double getInterestRate() const; // return interestRate
void displayAccountInformation() const; // display formatted account information through cout
// transformers
bool deposit(double amount);
// disposit amount into account
// return false if unsuccessful (amount invalid, balance not changed)
// true otherwise
bool withdraw(double amount);
// withdraw amount from account
// return false if unsuccessful (amount invalid, balance not changed)
// true otherwise
void applyInterest(); // apply interestRate to balance
bool setInterestRate(double newRate);
// change interestRate to newRate
// return false if unsuccessful (newRate invalid, interestRate not changed)
// true otherwise
bool setBalance(double newBalance);
// change balance to newBalance
// return false if unsuccessful (newBalance invalid, balance not changed)
// true otherwise
}; // end Account
#endif
这是Account.cpp 的代码
#include <iostream>
#include <iomanip>
#include "Account.h"
#include "ListDA.h"
using namespace std;
// constructor with default values
// set to default values if value(s) is/are not valid
Account::Account(double initialBalance = START_BALANCE, double initialInterestRate = START_INTEREST_RATE)
{
if (initialBalance >= 0)
balance = initialBalance;
else // initialBalance < 0
balance = START_BALANCE;
if (initialInterestRate >= 0 && initialInterestRate < 1)
interestRate = initialInterestRate;
else // initialInterestRate >= 1 or initialInterestRate < 0
interestRate = START_INTEREST_RATE;
} // end Account
// observers
double Account::getBalance() const
// return balance
{
return balance;
} // end getBalance
double Account::getInterestRate() const
// return interestRate
{
return interestRate;
} // end getInterestRate
void Account::displayAccountInformation() const
// display formatted account information through cout
{
cout << "Account Information" << endl
<< "Current Balance: $" << fixed << setprecision(2) << balance << endl
<< "Interest Rate: " << interestRate * 100 << "%" << endl;
} // end displayAccountInformation
// transformers
bool Account::deposit(double amount)
// disposit amount into account
// return false if unsuccessful (amount invalid, balance not changed)
// true otherwise
{
if (amount < 0) // invalid amount
return false;
balance += amount;
return true;
} // end deposit
bool Account::withdraw(double amount)
// withdraw amount from account
// return false if unsuccessful (amount invalid, balance not changed)
// true otherwise
{
if (amount < 0 || amount > balance) // invalid amount
return false;
balance -= amount;
return true;
} // end withdraw
void Account::applyInterest()
// apply interestRate to balance
{
balance *= 1.0 + interestRate;
} // end applyInterest
bool Account::setInterestRate(double newRate)
// change interestRate to newRate
// return false if unsuccessful (newRate invalid, interestRate not changed)
// true otherwise
{
if (newRate < 0 || newRate >= 1) // invalid newRate
return false;
interestRate = newRate;
return true;
} // end setInterestRate
bool Account::setBalance(double newBalance)
// change balance to newBalance
// return false if unsuccessful (newBalance invalid, balance not changed)
// true otherwise
{
if (newBalance < 0) // invalid balance
return false;
balance = newBalance;
return true;
} // end setBalance
任何有用的建议。谢谢
balance
是数据成员,对吗?对于balance
,您有一个getter和一个setter,对吧?
因此,除了可能使用int
或long
之外,您可以为帐号添加类似的项目。
然后,您需要决定如何初始化此新成员。一种方法是将其作为参数添加到构造函数中。
Lea,数据成员是看起来像double balance;
和double interestRate;
的行。它们只是数据,而不是像double getBalance() const;
或void applyInterest();
如果我理解正确,您想要某种唯一的帐户Id,它将成为类中的实例变量,对吗?你所拥有的是一个良好的开端,然后你只需在cunstructor中设置它,并创建像这样的set/get函数:
标题:
class Account {
private:
double accountId; // or number (which you should not call it. makes no sense)
....
public:
Account(...) {
this->accountId = some unique number or something...
}
double getAccountId() const {
return this->accountId; // number
}
void setAccountId(double aid) {
this->accountId = aid;
}
....
};
然而,如果你真的希望数字是唯一的,而不是由用户编辑(例如),我认为你不需要设置函数。
然后你必须问问自己,Id必须在多大程度上是唯一的。如果我们谈论的是少量的账户,而如果几个Id恰好相同,则不会造成大的损害,我不会在这上面浪费太多精力。例如,当苹果存储客户时,客户的Id必须尽可能地唯一,否则可能会发生很多可怕的事情。但是,如果您需要存储大型的唯一字符串,这可能会占用大量空间。
你可以使用rand()(http://www.cplusplus.com/reference/clibrary/cstdlib/rand/)生成一个随机的rumber,并将其与帐户关联的其他字符串/数字组合,或者只使用当前日期+时间(以毫秒为单位)。
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 如何将其他数据成员"number"添加到帐户类?
- 在这个"number of elements"宏中进行虚拟添加的目的是什么?