Question: (Removing Duplicated Code in the main Function) In Fig. 3.9, the main function contains six statements (lines 14-15, 16-17, 26-27, 28-29, 37-38, 39-40) that each

(Removing Duplicated Code in the main Function) In Fig. 3.9, the main function contains six statements (lines 14-15, 16-17, 26-27, 28-29, 37-38, 39-40) that each display an Account objects name and balance. Study these statements and youll notice that they differ only in the Account object being manipulated---acount1 or account2. In this exercise, youll define a new displayAccount function that contains one copy of that output statement. The member functions parameter will be an Account object and the member function will output the objects name and balance. Youll then replace the six duplicated statements in main with calls to displayAccount, passing as an argument the specific Account object to output.

Modify Fig. 3.9 to define the following displayAccount function after the using directive before main:

void displayAccount(Account accountToDisplay)

{

// place the statement that displays

// accountToDisplays name and balance here

}

Replace the comment in the member functions body with the statement that displays accountToDisplays name and balance.

Once youve completed displayAccounts declaration, modify main to replace the statements that display each Accounts name and balance with calls to displayAccount of the form:

displayAccount(nameOfAccountObject);

In each call, the argument should be the account1 or account2 object, as appropriate. Then, test the updated program to ensure that it produces the same output as shown in Fig. 3.9.

Here's the code for 3.9

__________________________.h file

#pragma once #include class account { public: account(std::string accountName, int initialBalance) :name{ accountName } {

if (initialBalance > 0) { balance = initialBalance; } }

void deposit(int depositAmount) { if (depositAmount > 0) { balance = balance + depositAmount; } }

int getBalance()const { return balance; }

void setName(std::string accountName) { name = accountName; }

std::string getName() const { return name; } private: std::string name; int balance{ 0 }; };

__________________________________.cpp

#include #include "account.h" #include "stdafx.h"

using namespace std;

int main() { account account1{ "Jane Green", 50 }; account account2{ "John Blue", -7 };

cout << "account1: " << account1.getName() << "balance is $" << account1.getBalance(); cout << " account2: " << account2.getName() << "balance is $" << account2.getBalance();

cout << " Enter deposit amount for account1: "; int depositAmount; cin >> depositAmount; cout << "adding " << depositAmount << " to account2 balance"; account1.deposit(depositAmount);

cout << " account1: " << account1.getName() << " balance is $" << account1.getBalance(); cout << " account2: " << account2.getName() << " balance is $" << account2.getBalance(); cout << "n Enter deposit amount for account2: "; cin >> depositAmount; cout << "adding " << depositAmount << " to account2 balance"; account2.deposit(depositAmount); cout << " account1: " << account1.getName() << " balance is $" << account1.getBalance(); cout << " account2: " << account2.getName() << " balance is $" << account2.getBalance() << endl;

system("pause");

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!