我一直在做一项琐碎的作业来习惯编码。我正在设计一台 ATM 机,目前它由 2 个类组成:
-
银行账户.cpp
-
交易.cpp
- 在 BankAccount 上执行一个方法(即存款、取款和获取余额)
Problem:BankAccount 自动初始化为余额 10,这是不希望的。例如,如果我开设了一个支票账户并选择存入 10 美元,则余额将打印出 20 美元。
//BankAccount.h
//This class will simply take in a bank account
//with a balance, other classes will use a bank account object
//such as saving/checkings and perform operations on the
//balance
#ifndef BANK_ACCOUNT_H
#define BANK_ACCOUNT_H
class BankAccount {
private:
float balance;
public:
BankAccount ();
float getBalance ();
void makeDeposit ();
void makeWithdrawl ();
};
#endif
//BankAccount.cpp
#include "BankAccount.h"
#include <iostream> //remove once done *not to self
using namespace std; //remove once done *note to self
BankAccount::BankAccount() {
balance = 0.00;
}
float BankAccount::getBalance() {
return balance;
}
void BankAccount::makeDeposit() {
cout << "How much would you like to deposit: ";
float deposit_value;
cin >> deposit_value;
balance += deposit_value;
}
void BankAccount::makeWithdrawl() {
cout << "How much would you like to withdrawl: ";
float withdrawl_value;
cin >> withdrawl_value;
balance -= withdrawl_value;
}
//Transaction.h
#ifndef TRANSACTION_H
#define TRANSACTION_H
class Transaction {
private:
BankAccount m_bao;
public:
Transaction(BankAccount&);
void displayOptions();
void printReciept();
};
#endif
//Transaction.cpp
#include "BankAccount.h"
#include "Transaction.h"
#include <iostream>
using namespace std;
Transaction::Transaction(BankAccount& bao) {
m_bao = bao;
}
void Transaction::displayOptions() {
cout << "\nPlease make a choice\n\n";
cout << "1: Make Deposit\n";
cout << "2: Make Withdrawl\n";
cout << "3: Check Balance\n";
int choice;
cin >> choice;
switch (choice) {
case 1:
m_bao.makeDeposit();
break;
case 2:
m_bao.makeWithdrawl();
break;
case 3:
m_bao.getBalance();
break;
}
}
void Transaction::printReciept() {
cout << "Current balance is now: " << m_bao.getBalance() + '\n';
}
int main () {
BankAccount checking;
Transaction q(checking);
q.displayOptions();
q.printReciept();
}
我确信答案就在我眼前,但我的大脑现在正处于煎炸状态。我将继续寻找解决方案,并让大家知道我的问题是否已经解决。
[EDIT]
好吧,现在我正在尝试让客户可以选择在支票帐户或储蓄帐户上执行交易。目前我在我的 main() 中看起来像这样:
int main () {
BankAccount checking(0.00);
BankAccount savings(0.00);
Transaction c(checking);
Transaction s(savings);
for(int i = 0; i < 10 ; i++) {
cout << "Make an option" << endl;
cout << "1. Checking " << endl;
cout << "2. Savings" << endl;
int choice;
cin >> choice;
if (choice == 1) {
c.prompt();
c.printReciept();
}
else {
s.prompt();
s.printReciept();
}
}
}
它工作得很好,但我想让这个过程更加 OOP 化,如果这有意义的话:)
我试图研究的一个选项是创建一个属于 Transaction.cpp 的提示函数。这将完成 main 中完成的所有操作,当然除了初始化对象之外。
你的问题是这一行:
cout << "Current balance is now: " << m_bao.getBalance() + '\n';
编译器将其视为:
cout << "Current balance is now: " << (m_bao.getBalance() + '\n');
'\n'
is 10
作为 int,所以你会得到:
cout << "Current balance is now: " << (m_bao.getBalance() + 10);
你可能想这样做:
cout << "Current balance is now: " << m_bao.getBalance() << '\n';
请记住,在 C++ 中,+
几乎总是意味着“将这两个数字相加”。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)