如何使方法更新C++中的类的属性

我想对银行中不同类型的账户进行一些基本的模拟。我已经创建了一个类AccountSavings_Account&Checking_Account是一个AccountandTrust_Account是一个Savings_Account. 我只在Account课堂上有问题,所以这个问题只会集中在Account课堂上。我在Account班级中只有两个属性:std::string namedouble balance. 我在Account课堂上也有两个方法:bool deposit(double amount)bool withdraw(double amount)问题:当我deposit(amount)工作正常并更新balance时,但当我withdraw(amount)更新时balance没有反映并amount退出默认余额。

我的代码:

帐户.hpp

#pragma once

#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

//The Account class(parent class)
class Account{
    friend std::ostream &operator<<(std::ostream &os, const Account &account);  //Outputs the object
private:
    static constexpr const char *def_name = "Empty";              //Default name
    static constexpr double def_balance = 0.0;                    //Default balance
protected:
    string name{};                                          //Name of the Account holder
    double balance{};                                       //Balance of the Account
public:
    Account(string n = def_name, double b = def_balance);        //Constructor
    bool deposit(double amount);                                 //Method to deposit 
    bool withdraw(double amount);                                //Method to withdraw
};

帐户.cpp

#include "Accounts.hpp"

//Construtor
Account::Account(string n, double b) 
    :name{n}, balance{b} {
}

//Method for deposit
bool Account::deposit(double amount) {
    if (amount < 0)
        return false;
    else {
        balance += amount;
        return true;
    }
}

//Method for withdraw
bool Account::withdraw(double amount) {
    if (balance - amount >= 0) {
        balance -= amount;
        return true;
    } else {
        return false;
    }
}

//Outputs the object
std::ostream &operator<<(std::ostream &os, const Account &account) {
    os << "A-> " << account.name << " : " << account.balance << endl;
    return os;
}

我也做了一些实用功能,让我displaydeposit以及withdraw所有Accountvector<Account> acnt

AccountsUtil.hpp

#include "Accounts.hpp"

/* Util functions for Account */
//Displays all the Account
void display(const vector<Account> &a);

//Deposits to all the Account
void deposit(vector<Account> &a, double bl);

//Withdraws from all the Account
void withdraw(vector<Account> &a, double bl);

AccountsUtil.cpp

/* Utils for Account */
//Displays all the Account
void display(const vector<Account> &a) {
    cout << "--->Accounts----------------" << endl;
    for (const auto &i: a)
        cout << i << endl;
    cout << "=====================================n" << endl;
}

//Deposits to all the Account
void deposit(vector<Account> &a, double bl) {
    cout << "Depositing for Account=========================" << endl;
    for (auto i: a) {
        if (i.deposit(bl))
            cout << "Deposit successful for:=> " << i << endl;
        else   
            cout << "Deposit failed for:=> " << i << endl;
    }
    cout << "==============================================n" << endl;
}

//Withdraws from all the Account
void withdraw(vector<Account> &a, double bl) {
    cout << "Withdrawing for Account============================" << endl;
    for (auto i: a) {
        if (i.withdraw(bl))
            cout << "Withdraw successful for :=> " << i << endl;
        else    
            cout << "Withdraw failed for :=> " << i << endl;
    }
    cout << "===================================================n" << endl;
}

主程序

#include "AccountsUtil.hpp" 

int main() {
    vector<Account> acnt;
    acnt.push_back(Account {});
    acnt.push_back(Account {"Tony"});
    acnt.push_back(Account {"Steve", 2000});
    acnt.push_back(Account {"Thor", 5000});

    display(acnt);
    deposit(acnt,1000);
    withdraw(acnt,3000);

    return 0;
}

我的输出(错误/意外)

--->Accounts----------------
A-> Empty : 0

A-> Tony : 0

A-> Steve : 2000

A-> Thor : 5000

=====================================

Depositing for Account=========================
Deposit successful for:=> A-> Empty : 1000

Deposit successful for:=> A-> Tony : 1000

Deposit successful for:=> A-> Steve : 3000

Deposit successful for:=> A-> Thor : 6000

==============================================

Withdrawing for Account============================
Withdraw failed for :=> A-> Empty : 0

Withdraw failed for :=> A-> Tony : 0

Withdraw failed for :=> A-> Steve : 2000

Withdraw successful for :=> A-> Thor : 2000

===================================================

很明显,存款时余额已更新,但在提取时使用了默认余额。例如:在存入史蒂夫的余额为 3000 后,因此从史蒂夫那里提取 3000 应该可以正常工作,但对他而言提取失败表明他只有 2000,这是他的默认余额。我怎么解决这个问题?谢谢。

回答

问题是depositwithdraw函数中的循环:

for (auto i: a)

这里i将是vector 中元素的副本a。所有修改(如调用i.deposit(bl))将仅在该副本上进行,而不是在向量中的元素上进行。

您需要使用向量中元素的引用进行迭代:

// Note ampersand to make this a reference
//       v
for (auto& i: a)


以上是如何使方法更新C++中的类的属性的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>