int _tmain(int argc, _TCHAR* argv[]) { double balance = 500; double deposit, withdraw; // set floating-point output formatting cout << fixed << setprecision(2); // Vector of 2 base class pointers vector <Account *> accounts(2); // Initialize vectors with various accounts accounts[0] = new SavingAccount(balance, 15); accounts[1] = new CheckingAccount(balance, 2.50); // polymorphically process each element in vector employees for (size_t i = 0; i < accounts.size(); i++) { // Downcast pointer SavingAccount *derivedPtr = dynamic_cast <SavingAccount *>(accounts[i]); if (derivedPtr != 0) // 0 if not CheckingAccount { system("cls"); cout << "Account: " << typeid(*accounts[i]).name(); // Calculate interest on existing balance cout << "\n\nWith an interest rate of 15%, you have accumulated: $" << derivedPtr->calculateInterest(); cout << "\nCurrent Account Balance: $" << derivedPtr->credit(); // Depositing funds into account cout << "\n\nEnter Deposit Amount: $"; cin >> deposit; derivedPtr->setCreditAmount(deposit); derivedPtr->credit(); cout << "Updated Account Balance: $" << derivedPtr->getAccountBalance(); // Withdrawing funds from account cout << "\n\nEnter Withdraw Amount: $"; cin >> withdraw; derivedPtr->setDebitAmount(withdraw); derivedPtr->debit(); cout << "Updated Account Balance: $" << derivedPtr->getAccountBalance() << endl << endl;; system("pause"); } // This falls into CheckingAccount else {
int main() { Account a = Account(100.00); SavingAccount sa = SavingAccount(1000.00, 0.03); CheckingAccount ca = CheckingAccount(3000.00, 100.00); a.credit(100.00); cout << a.getBalance() << endl; a.debit(50.00); cout << a.getBalance() << endl; sa.credit(1000.00); cout << sa.getBalance() << endl; sa.calculateInterest(); sa.debit(1000); cout << sa.getBalance() << endl; ca.credit(10.00); cout << ca.getBalance() << endl; ca.debit(1000.00); cout << ca.getBalance() << endl; return 0; }