与构造函数,类和单独文件混淆

时间:2013-09-18 01:01:34

标签: c++ class constructor

我应该编写一个主程序,提示用户输入名称和初始余额。您的程序应该创建两个对象,一个使用默认构造函数,另一个使用构造函数,该构造函数允许用户输入的名称和余额用于初始化其对象。然后提示用户将一笔金额记入一个帐户并将一笔金额记入借方。使用creditAccount将资金存入使用参数化构造函数创建的对象,并使用debitAccount从使用默认构造函数创建的对象中减去借方金额。使用displayBalance在两个对象中显示余额。重复上述循环,直到用户输入退出程序的选项。 我只是不明白从哪里开始实际构建和使用该类。 Main.cpp的

//9/17/2013
//
//I have read and understand the Lab Submittal Policy document on BB.

#include <iostream>
#include <sstream>
#include <string>
#include "Account.h"

using namespace std;

int main()
{
    char Redo;
    string CustomerName;

do
{
    float InitialBalance = -1;
    float balance1 = 0;
    float balance2 = 0;


    Account Account;
    Account.CreditAccount (balance1, InitialBalance);
    Account.DebitAccount (balance2, InitialBalance);
    Account.DisplayBalance (CustomerName, balance1, balance2);

    //Asks user if they want redo the program
    cout << "Would you like to redo the program?\n";
    cout << "Please enter Y or N: \n \n";
    cin >> Redo;
}while(Redo == 'Y' || Redo == 'y');


char exitchar; //Exit's the program.
cout << "\nPress any key and <enter> to exit the program.\n";
cin >> exitchar;

return 0;
}

Account.h

using namespace std;

class Account {
public:
    float balance1;
    float balance2;
    string CustomerName;
    float InitialBalance;
    float CreditAccount(float& balance1, float InitialBalance);
    float DebitAccount(float& balance2, float InitialBalance);
    float DisplayBalance(string CustomerName, float balance1, float balance2);
    Account ();


    Account(float balance)
    {
        SetInitialBalance(balance);
    }
    void Account::SetInitialBalance(float balance)
    {
        if(balance >= 0)
        {
            InitialBalance = balance;
        }
        else
               cout << "Error! Initial Balance cannot be less than 0." << endl;
    }
};

Account::Account(void)
{
string CustomerName;

cout << "Your Account Machine" << endl;
cout << "Please enter your last name." << endl;
cin >> CustomerName;
while(InitialBalance < 0)
{
cout << "Please enter your account balance. No Commas." << endl;
cin >> InitialBalance;
if(InitialBalance < 0)
    cout << "Error account balance must be positive." << endl;
}
}

float Account::CreditAccount(float& balance1, float InitialBalance)
    {
        float CreditInput = -1;
        while(CreditInput<0){
        cout << "Would you like to credit the account? Enter the amount you would like to credit." << endl;
        cin >> CreditInput;
        if (CreditInput<0)
            cout << "Credit must be positive." << endl;
        }
        balance1 = (CreditInput + InitialBalance);
        return balance1;
    }
float Account::DebitAccount(float& balance2, float InitialBalance)
    {
        float DebitInput = 0;
        while((InitialBalance - DebitInput)<0){
        cout << "Would you like to debit the account? Enter the amount you would like to debit." << endl;
        cin >> DebitInput;
        if((InitialBalance-DebitInput)<0)
            cout << "You cannot debit more than you have avalaible." << endl;
        }
        balance2 = (InitialBalance - DebitInput);
        if( DebitInput > InitialBalance)
        {
        cout << "Debit amount exceeds account balance." << endl;
        return 0;
        }
        else 
            return balance2;
    }
float Account::DisplayBalance(string CustomerName, float balance1, float balance2)
    {
        cout << "Customer Name: " << CustomerName << endl;
        cout << "Account Balance for credited account: " << balance1 << endl;
        cout << "Account Balance for debited account: " << balance2 << endl;
        return 0;
    }

已更新我更新了我的代码,我只有一个问题。我想知道为什么我的程序没有读取任何CustomerName或InitialBalance。其他一切都符合我的喜好。

再次感谢你!

1 个答案:

答案 0 :(得分:1)

我研究了构造函数,类和单独的文件。我学会了如何创建一个对象,并从不同文件中的其他类调用成员函数。在我提出这个问题之前,我应该对此进行更多的研究。