如何将其他数据成员"number"添加到帐户类?

How to add an additional data member "number" to Account class?

本文关键字:添加 number 其他 数据成员      更新时间:2023-10-16

我想我对语法有点困惑。我需要帮助添加一个名为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,对吧?

因此,除了可能使用intlong之外,您可以为帐号添加类似的项目。

然后,您需要决定如何初始化此新成员。一种方法是将其作为参数添加到构造函数中。

是的,当然是家庭作业。。。尽管如此,我还是要帮Lea一把。

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,并将其与帐户关联的其他字符串/数字组合,或者只使用当前日期+时间(以毫秒为单位)。