Tuesday, 17 September 2013

Confusion with Constructors, Classes, and separate files

Confusion with Constructors, Classes, and separate files

I'm supposed to write a main program that prompts the user for a name and
initial balance. Your program should create two objects, one using the
default constructor and another using the constructor that allows the
user-entered name and balance to be used to initialize its object. Then
prompt the user for one amount to be credited to an account and one amount
to be debited. Use creditAccount to credit money to the object created
with the parameterized constructor and debitAccount to subtract the debit
amount from the object created with the default constructor. Use
displayBalance to display the balance in both objects. Repeat the cycle
above until the user enters an option to exit the program. I just don't
understand where to go from here to have it actually build and use the
class.
//My Account Program
//9/17/2013
#include <iostream>
#include <sstream>
#include <string>
#include "Account.h"
using namespace std;
int main()
{
float balance;
string CustomerName;
cout << "Your Account Machine" << endl;
cout << "Please enter your last name." << endl << endl;
cin >> CustomerName;
cout << "Please enter your account balance." << endl;
cin >> balance;
Account balance();
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:
Account();
float InitialBalance;
Account::Account(float balance)
{
SetInitialBalance(balance);
}
void SetInitialBalance(float balance)
{
if(balance >= 0)
InitialBalance = balance;
else
cout << "Error! Initial Balance cannot be less than 0." << endl;
}
float CreditAccount(float& balance)
{
float CreditInput;
cout << "Would you like to credit the account? Enter the amount
you would like to credit." << endl;
cin >> CreditInput;
balance = (CreditInput + balance);
}
float DebitAccount(float& balance)
{
float DebitInput;
cout << "Would you like to debit the account? Enter the amount you
would like to debit." << endl;
cin >> DebitInput;
balance = (balance - DebitInput);
if( balance < 0)
cout << "Debit amount exceeds account balance." << endl;
}
float DisplayBalance(float balance)
{
string CustomerName;
cout << "Customer Name: " << CustomerName << endl;
cout << "Account Balance: " << balance << endl;
}
};

No comments:

Post a Comment