I wrote up the code for the class, despite not really knowing what a constructor truely is I went ahead and took a wild guess. When I got to main and created the class objects (account bal, account acc) the compiler gives me errors about no matching function for the call to the constructor.
I am thinking that I am not using the contructor properly.Code:#include <iostream> using namespace std; class Account { public: Account(int); //Constructor int Debit(int); //Member Function int Credit(int); //Member Function int getBalance(); //Member Function private: int Balance; //= 1000; //Data Member }; /*Account constructor makes sure there are no overdraws and the account balance is always zero or above */ Account::Account(int AmtDebit){ if (Balance < AmtDebit){ cout <<"Debit ammount exceeded account balance" <<endl; } if (Balance < 0){ cout <<"Initial account balance invalid" <<endl; Balance = 0; } } // Account debit member function declaration. Asks for amount to debit and subtracts from balance int Account::Debit(int AmtDebit){ cout <<"How much do you want to withdraw?" <<endl; cin >>AmtDebit; Balance = Balance - AmtDebit; cout <<"The new account balance is " <<Balance <<endl; } // Account creddit member function declaration. Asks for amount to credit and adds to balance int Account::Credit(int AmtCredit){ cout <<"How much do you want to deposit?" <<endl; cin >>AmtCredit; Balance = Balance + AmtCredit; cout <<"The new account balance is " <<Balance <<endl; } // Output current balance int Account::getBalance() { cout <<"Account balance is " <<Balance <<endl; } int main(){ Account acc; // Account object to test debit and credit Account bal; // Account object to test balance member function and data member int x; // Menu option control variable int amount; // Amount to credit/debit cout <<"====================================================" <<endl; cout <<"= Welcome to your ATM =" <<endl; cout <<"====================================================" <<endl; cout <<"+ 1. View balance +" <<endl; cout <<"+ 2. Withdraw +" <<endl; cout <<"+ 3. Deposit +" <<endl; cout <<"+ 4. Exit +" <<endl; cout <<"++++++++++++++++++++++++++++++++++++++++++++++++++++" <<endl; cout <<"Select an option (1-4) and press enter." <<endl; cin >>x; // View balance if (x == 1) { bal.getBalance(); } if (x == 2) { cout <<"How much do you wish to withdraw?" <<endl; cin >>amount; acc.Debit(amount); } if (x == 3) { cout <<"How much do you with to deposit?" <<endl; cin >>amount; acc.Credit(amount); } if (x == 4) { cout <<"Thank you! Come again!" <<endl; } if (x > 4) { cout <<"Invalid option, please try again." <<endl; cin >>x; } return 0; }
NOTE** I realize that I can make this code a lot shorter but the directions forbid me to use anything the class hasn't taught me.