c++ - 与构造函数、类和单独的文件混淆

标签 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;
}

账户.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。其他一切都符合我的喜好。

再次感谢!

最佳答案

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

关于c++ - 与构造函数、类和单独的文件混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18862247/

相关文章:

java - 如果提供了某些参数,是否可以禁止创建类对象?

java - 哪个模板是 netbeans 默认的新 java 类?

c++ - 抽象类和唯一指针

c++ - 将未知文件格式打印到屏幕

c++ - 全局范围内变量和函数的递归依赖

python - * : 'instance' and 'float' 不支持的操作数类型

c# - 我可以将参数从派生类的默认构造函数传递给基构造函数吗?

c++ - 在 C++ 中为没有默认构造函数的成员复制构造函数

c++ - 为什么我们实际上需要运行时多态性?

c++ - 使用凭据提供程序登录