I would try to focus on what these programs are doing and how they work. Not too much on the do it yourself here for the longer ones. (Fix spacing in code)
Class file - in class
// Modified Account.h
// Account class that contains a name data member,
// prototypes of member functions to set and get its value,
// and 2 constructors. One constructor initializes the
// account name to the provided string. The other
// constructor initializes the account name to the
// empty string. See Account.cc for the constructor
// implementations.
class Account {
public:
// constructor with zero parameters
Account();
// constructor that initializes data member name
Account(std::string);
// member function that sets the account name in the object
void setName(std::string);
// member function that retrieves the account name from the object
std::string getName() const;
private:
std::string name; // data member containing account holder's name
}; // end class Account
Implementation of account class - in class
// Implementation of Account class with 2 constructors
#include <string> // enable program to use C++ string data type
#include "Account.h"
using namespace std;
Account::Account()
{
name = "";
}
Account::Account(string accountName) : name{accountName} // member initializer
{
// empty body
}
void Account::setName(string accountName)
{
name = accountName;
}
string Account::getName() const
{
return name;
}
Test to make sure they work - in class
// Modified Fig. 3.5: AccountTest.cc
// Using the Account constructor to initialize the name data
// member at the time each Account object is created.
// This version uses 2 different constructors, one with a
// single string parameter and the other with zero parameters.
#include <iostream>
#include "Account.h"
using namespace std;
int main() {
// create two Account objects
Account account1{"Jane Green"};
Account account2{"John Blue"};
Account myAccount;
// display initial value of name for each Account
cout << "account1 name is: " << account1.getName() << endl;
cout << "account2 name is: " << account2.getName() << endl;
cout << "other account name is: " << myAccount.getName() << endl;
}
Book example of above
// Fig. 3.8: Account.h
// Account class with name and balance data members, and a
// constructor and deposit function that each perform validation.
#include <string>
class Account {
public:
// Account constructor with two parameters
Account(std::string accountName, int initialBalance)
: name{accountName} { // assign accountName to data member name
// validate that the initialBalance is greater than 0; if not,
// data member balance keeps its default initial value of 0
if (initialBalance > 0) { // if the initialBalance is valid
balance = initialBalance; // assign it to data member balance
}
}
// function that deposits (adds) only a valid amount to the balance
void deposit(int depositAmount) {
if (depositAmount > 0) { // if the depositAmount is valid
balance = balance + depositAmount; // add it to the balance
}
}
// function returns the account balance
int getBalance() const {
return balance;
}
// function that sets the name
void setName(std::string accountName) {
name = accountName;
}
// function that returns the name
std::string getName() const {
return name;
}
private:
std::string name; // account name data member
int balance{0}; // data member with default initial value
}; // end class Account
Test to see it works - book example
// Fig. 3.9: AccountTest.cpp
// Displaying and updating Account balances.
#include <iostream>
#include "Account_text_book.h"
using namespace std;
int main()
{
Account account1{"Jane Green", 50};
Account account2{"John Blue", -7};
// display initial balance of each object
cout << "account1: " << account1.getName() << " balance is $"
<< account1.getBalance();
cout << "\naccount2: " << account2.getName() << " balance is $"
<< account2.getBalance();
cout << "\n\nEnter deposit amount for account1: "; // prompt
int depositAmount;
cin >> depositAmount; // obtain user input
cout << "adding " << depositAmount << " to account1 balance";
account1.deposit(depositAmount); // add to account1's balance
// display balances
cout << "\n\naccount1: " << account1.getName() << " balance is $"
<< account1.getBalance();
cout << "\naccount2: " << account2.getName() << " balance is $"
<< account2.getBalance();
cout << "\n\nEnter deposit amount for account2: "; // prompt
cin >> depositAmount; // obtain user input
cout << "adding " << depositAmount << " to account2 balance";
account2.deposit(depositAmount); // add to account2 balance
// display balances
cout << "\n\naccount1: " << account1.getName() << " balance is $"
<< account1.getBalance();
cout << "\naccount2: " << account2.getName() << " balance is $"
<< account2.getBalance() << endl;
}