Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// This program introduces the use of static member variables. C++ #include <ios

ID: 3836017 • Letter: #

Question

// This program introduces the use of static member variables. C++

#include <iostream>

using namespace std;

class SavingsAcct

{

private:

int acctNum; // "regular" member variables

double balance;

// Fill in the code to declare a static int variable named

// nextAcctNumber.

// Fill in the code to declare a static double variable named

// totalDeposits.

public:

SavingsAcct(); // default constructor

SavingsAcct(double); // constructor

void newAcctInfo();

double getTotalDeposits();

void displayAcctInfo();

void deposit(double);

void withdraw(double);

};

// Static Variable Definitions

// Fill in the code to define the static variable nextAcctNumber and

// initialize it to 5000.

// Fill in the code to define the static variable totalDeposits and initialize

// it to 0.

// Member Function Implementation Section

SavingsAcct::SavingsAcct() // default constructor

{

// Fill in the code to assign acctNum the next available account number.

// Fill in the code to increment the next available account number.

balance = 0;

newAcctInfo();

}

SavingsAcct::SavingsAcct(double startBal) // constructor

{ // Fill in the code to assign acctNum the next available account number.

// Fill in the code to increment the next available account number.

// Fill in the code to set balance to the starting balance passed in.

// Fill in the code to add this account's starting balance to the static

// variable named totalDeposits.

newAcctInfo();

}

void SavingsAcct::newAcctInfo()

{ cout << "New account number: " << acctNum << " Initial Balance: $"

<< balance << endl;

}

double SavingsAcct::getTotalDeposits()

{ return totalDeposits;

}

void SavingsAcct::displayAcctInfo()

{

// Fill in the code to display the account's account number and balance.

}

void SavingsAcct::deposit(double amt)

{

// Fill in the code to implement this function. The amount passed in must

// be added to the account's balance. Remember that any change to an

// account's balance also affects totalDeposits.

}

void SavingsAcct::withdraw(double amt)

{ // This function is not being implemented at this time.

}

// Client code

int main()

{

// Fill in the code to create acct1 with a starting balance of $100.

// Fill in the code to create acct2 with a starting balance of $250.

// Fill in the code to deposit $50 in acct2.

// Fill in the code to display the account information for acct1.

// Fill in the code to display the account information for acct2.

// Fill in the code to display totalDeposits.

return 0;

}

Exercise 1: Complete the program as directed in the bold comments. The following should be the

output:

New account number: 5000 Initial Balance: $100

New account number: 5001 Initial Balance: $250

Account number 5000 has a balance of $100

Account number 5001 has a balance of $300

Total savings deposits =$400

Exercise 2: Notice that the newAcctInfo() function is never called by any user function outside

the class. It is called only by other class functions. This means it can be a private member

function. Modify the program you completed in Exercise 1 to move the declaration of this

member function from the public section to the private section and then rerun this program. You

should get the same results.

Exercise 3: Modify the program you created in Exercise 2 to add a static void public

function named firstAcctNumber which allows the starting account number to be input to the

program , rather than hard coded within it. Write the function prototype and implementation for

this function. The static variable nextAcctNumber will still need to be defined, as it was before,

but it will not be initialized when it is defined. Instead, before creating any accounts, the main

function should have the user input the starting account number and then call your

fistAcctNumber function to initialize it with the input number. After making these modifications

run your program again, inputting 5000 as the starting account number. The results should be the

same as those shown in exercise 1.

Explanation / Answer

Exercise 1:

// This program introduces the use of static member variables. C++
#include <iostream>
using namespace std;
class SavingsAcct
{
private:
int acctNum; // "regular" member variables
double balance;
static int nextAcctNumber;
static double totalDeposits;

public:
SavingsAcct(); // default constructor
SavingsAcct(double); // constructor
void newAcctInfo();
double getTotalDeposits();
void displayAcctInfo();
void deposit(double);
void withdraw(double);
};
// Static Variable Definitions
// Fill in the code to define the static variable nextAcctNumber and
// initialize it to 5000.
int SavingsAcct::nextAcctNumber = 5000;
// Fill in the code to define the static variable totalDeposits and initialize
// it to 0.
double SavingsAcct::totalDeposits = 0;

// Member Function Implementation Section
SavingsAcct::SavingsAcct() // default constructor
{
// Fill in the code to assign acctNum the next available account number.
acctNum = nextAcctNumber;
// Fill in the code to increment the next available account number.
nextAcctNumber++;
balance = 0;
newAcctInfo();
}
SavingsAcct::SavingsAcct(double startBal) // constructor
{ // Fill in the code to assign acctNum the next available account number.
acctNum = nextAcctNumber;
// Fill in the code to increment the next available account number.
nextAcctNumber++;
// Fill in the code to set balance to the starting balance passed in.
balance = startBal;
// Fill in the code to add this account's starting balance to the static
// variable named totalDeposits.
totalDeposits += startBal;
newAcctInfo();
}
void SavingsAcct::newAcctInfo()
{ cout << "New account number: " << acctNum << " Initial Balance: $"
<< balance << endl;
}
double SavingsAcct::getTotalDeposits()
{ return totalDeposits;
}
void SavingsAcct::displayAcctInfo()
{
// Fill in the code to display the account's account number and balance.
cout << "Account number "<<acctNum<<" has a balance of $"<<balance<<endl;
}
void SavingsAcct::deposit(double amt)
{
// Fill in the code to implement this function. The amount passed in must
// be added to the account's balance. Remember that any change to an
// account's balance also affects totalDeposits.
balance += amt;
totalDeposits += amt;

}
void SavingsAcct::withdraw(double amt)
{ // This function is not being implemented at this time.
}
// Client code
int main()
{
// Fill in the code to create acct1 with a starting balance of $100.
SavingsAcct acct1 = SavingsAcct(100);
// Fill in the code to create acct2 with a starting balance of $250.
SavingsAcct acct2 = SavingsAcct(250);
// Fill in the code to deposit $50 in acct2.
acct2.deposit(50);
// Fill in the code to display the account information for acct1.
acct1.displayAcctInfo();
// Fill in the code to display the account information for acct2.
acct2.displayAcctInfo();
// Fill in the code to display totalDeposits.
SavingsAcct* t;
cout<<"Total savings deposits=$"<<t->getTotalDeposits()<<endl;
return 0;
}

Sample Output:

New account number: 5000 Initial Balance: $100
New account number: 5001 Initial Balance: $250
Account number 5000 has a balance of $100
Account number 5001 has a balance of $300
Total savings deposits =$400

Exercise 2:

// This program introduces the use of static member variables. C++
#include <iostream>
using namespace std;
class SavingsAcct
{
private:
int acctNum; // "regular" member variables
double balance;
static int nextAcctNumber;
static double totalDeposits;
void newAcctInfo();

public:
SavingsAcct(); // default constructor
SavingsAcct(double); // constructor
double getTotalDeposits();
void displayAcctInfo();
void deposit(double);
void withdraw(double);
};
// Static Variable Definitions
// Fill in the code to define the static variable nextAcctNumber and
// initialize it to 5000.
int SavingsAcct::nextAcctNumber = 5000;
// Fill in the code to define the static variable totalDeposits and initialize
// it to 0.
double SavingsAcct::totalDeposits = 0;

// Member Function Implementation Section
SavingsAcct::SavingsAcct() // default constructor
{
// Fill in the code to assign acctNum the next available account number.
acctNum = nextAcctNumber;
// Fill in the code to increment the next available account number.
nextAcctNumber++;
balance = 0;
newAcctInfo();
}
SavingsAcct::SavingsAcct(double startBal) // constructor
{ // Fill in the code to assign acctNum the next available account number.
acctNum = nextAcctNumber;
// Fill in the code to increment the next available account number.
nextAcctNumber++;
// Fill in the code to set balance to the starting balance passed in.
balance = startBal;
// Fill in the code to add this account's starting balance to the static
// variable named totalDeposits.
totalDeposits += startBal;
newAcctInfo();
}
void SavingsAcct::newAcctInfo()
{ cout << "New account number: " << acctNum << " Initial Balance: $"
<< balance << endl;
}
double SavingsAcct::getTotalDeposits()
{ return totalDeposits;
}
void SavingsAcct::displayAcctInfo()
{
// Fill in the code to display the account's account number and balance.
cout << "Account number "<<acctNum<<" has a balance of $"<<balance<<endl;
}
void SavingsAcct::deposit(double amt)
{
// Fill in the code to implement this function. The amount passed in must
// be added to the account's balance. Remember that any change to an
// account's balance also affects totalDeposits.
balance += amt;
totalDeposits += amt;

}
void SavingsAcct::withdraw(double amt)
{ // This function is not being implemented at this time.
}
// Client code
int main()
{
// Fill in the code to create acct1 with a starting balance of $100.
SavingsAcct acct1 = SavingsAcct(100);
// Fill in the code to create acct2 with a starting balance of $250.
SavingsAcct acct2 = SavingsAcct(250);
// Fill in the code to deposit $50 in acct2.
acct2.deposit(50);
// Fill in the code to display the account information for acct1.
acct1.displayAcctInfo();
// Fill in the code to display the account information for acct2.
acct2.displayAcctInfo();
// Fill in the code to display totalDeposits.
SavingsAcct* t;
cout<<"Total savings deposits=$"<<t->getTotalDeposits()<<endl;
return 0;
}

Sample Output:

New account number: 5000 Initial Balance: $100
New account number: 5001 Initial Balance: $250
Account number 5000 has a balance of $100
Account number 5001 has a balance of $300
Total savings deposits =$400