对象正在初始化为不需要的值 [英] Object is initializing to unwanted value

查看:157
本文介绍了对象正在初始化为不需要的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一个简单的任务,习惯于编码。我正在设计一个ATM机,目前它由两个类组成:


  1. BankAccount.cpp




    • 不同类型帐户的构造函数

    • 只有作为成员的余额


      • Transaction.cpp




        • 对BankAccount执行方法

问题: BankAccount自动初始化为10的余额,这是不受欢迎的。例如,如果我做了支票帐户并选择存入$ 10,余额将打印出$ 20。

  // BankAccount。 h 
//这个类将简单地带入一个带有余额的银行帐户
//,其他类将使用一个银行帐户对象
//,例如保存/检查,并对
// 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
#includeBankAccount.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< 你想要多少钱:;
float deposit_value;
cin>> deposit_value;
balance + = deposit_value;
}

void BankAccount :: makeWithdrawl(){
cout< 你想要多少钱:;
float withdrawl_value;
cin>> withdrawl_value;
balance - = withdrawl_value;
}

//Transaction.h
#ifndef TRANSACTION_H
#define TRANSACTION_H

类事务{
private:
BankAccount m_bao;
public:
Transaction(BankAccount&);
void displayOptions();
void printReciept();
};

#endif

//Transaction.cpp
#includeBankAccount.h
#includeTransaction.h
# include< iostream>
using namespace std;

Transaction :: Transaction(BankAccount& bao){
m_bao = bao;
}

void Transaction :: displayOptions(){
cout< \\\
请选择\\\
\\\
;
cout<< 1:存款
cout<< 2:Make Withdrawl\\\
;
cout<< 3:检查余额\\\
;

int选择;
cin>>选择;
switch(choice){
case 1:
m_bao.makeDeposit();
break;
case 2:
m_bao.makeWithdrawl();
break;
case 3:
m_bao.getBalance();
break;
}
}

void Transaction :: printReciept(){
cout< 当前余额现在是:< m_bao.getBalance()+'\\\
';
}


int main(){

BankAccount checking;
事务q(检查);
q.displayOptions();
q.printReciept();


}

我相信答案是正确的我的眼睛前面,但我的大脑只是油炸了现在。我会继续寻找解决方案,让你们知道我的问题是否已经解决了。





好的,现在我想让客户可以选择在支票或储蓄帐户上执行交易。目前我在main()中看起来像这样:

  int main(){

BankAccount checks(0.00);
BankAccount Saving(0.00);
事务c(检查);
交易(储蓄);
for(int i = 0; i <10; i ++){
cout< 选择<< endl
cout<< 1.检查< endl
cout<< 2.储蓄< endl

int选择;
cin>>选择;
if(choice == 1){
c.prompt();
c.printReciept();
}
else {
s.prompt();
s.printReciept();
}
}

}



它工作正常,但我想让这个过程更OOP-alized,如果这是有道理的)



一个选项我试图看into是一个提示函数,属于Transaction.cpp。

解决方案

你的问题是这一行:

  cout< 当前余额现在是:< m_bao.getBalance()+'\\\
';

编译器看到的是:

  cout<< 当前余额现在是:< (m_bao.getBalance()+'\\\
');

'\\\
'
c $ c> 10 作为int,所以你得到这个:

  cout< 当前余额现在是:< (m_bao.getBalance()+ 10); 

您可能希望这样做:

  cout<< 当前余额现在是:< m_bao.getBalance()<< '\\\
';

请记住,在C ++中, + 表示添加这两个数字。


I have been working on a trivial assignment to get used to coding. I am designing an ATM machine and at the moment it is composed of 2 classes:

  1. BankAccount.cpp

    • Constructor for different types of account
    • Only has balance as a member
  2. Transaction.cpp

    • Performs a method on the BankAccount (i.e make deposit, make withdrawl & get balance)

Problem: BankAccount is automatically initialized to a balance of 10 which is undesired. So for example, if I made a checking account and chose to deposit $10, balance would print out $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();


}

I am sure the answer is right in front of my eyes, but my brain is just fried right now. I will continue to look for the solutions and let you guys know if my problem has been solved yet.

[EDIT]

Alright, now I am trying to make it so that the customer can choose to perform transactions on either Checking or Savings account. Currently I got it looking like this in my 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();
        }
    }

}

It works fine, but I would like to make this process more OOP-alized, if that makes sense :)

One option I was trying to look into was making a prompt function which would belong to Transaction.cpp. This would do everything that is done in main, except initializing the objects of course.

解决方案

Your problem is this line:

cout << "Current balance is now: " << m_bao.getBalance() + '\n';

Which the compiler sees as:

cout << "Current balance is now: " << (m_bao.getBalance() + '\n');

'\n' is 10 as an int, so you get this:

cout << "Current balance is now: " << (m_bao.getBalance() + 10);

You probably meant to do this:

cout << "Current balance is now: " << m_bao.getBalance() << '\n';

Remember that in C++, + almost always means "add these two numbers".

这篇关于对象正在初始化为不需要的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆