Figure 1.9 Financial Aid Update Function |
#include <cassert>
void updateFinancialAid(int numRecords, StudentAidRecord studentRecord[],
double percent)
/*--------------------------------------------------------------------------
Increase the amount of all financial aid awards in an array of
student financial aid records by a specified percentage.
Precondition: numRecords > 0 and percent > 0 is a expressed
as a decimal.
Postcondition: Each record in finAidArray has been modified by
increasing the amount of each financial aid award in each
record by the specified percentage.
-------------------------------------------------------------------------*/
{
assert (numRecords > 0 && percent > 0);
for (int record = 0; record < numRecords; record++)
{
int awardCount = studentRecord[record].getNumAwards();
for (int count = 0; count < awardCount; count++)
{
FinancialAidAward aid = studentRecord[record].getFinancialAid(count);
double newAmount = aid.getAmount();
newAmount += percent * newAmount;
aid.setAmount(newAmount);
studentRecord[record].setFinancialAid(count, aid);
}
}
}
|
Figure 1.10 Test-Driver for Financial Aid Update Function |
#include <iostream>
#include <string>
using namespace std;
#include "FinancialAidAward.h" // Financial aid awards
#include "StudentAidRecord.h" // Student financial-aid records
//-- Prototype of updateFinancialAid(); definition follows main()
void updateFinancialAid(int numRecords, StudentAidRecord studentRecord[],
double percent);
int main()
{
const int NUM_RECORDS = 3;
StudentAidRecord arr[NUM_RECORDS];
double percent = .10;
int id, awards;
string name, source;
double amount;
for (int i = 0; i < NUM_RECORDS; i++)
{
cout << "\nEnter student's id, name: ";
cin >> id;
getline(cin, name);
arr[i].setId(id);
arr[i].setName(name);
cout << "Enter number of awards for " << id << ": ";
cin >> awards;
arr[i].setNumAwards(awards);
for (int a = 0; a < awards; a++)
{
cout << "Award " << a + 1 << "'s amount and source: ";
cin >> amount;
getline(cin, source);
FinancialAidAward finaid(source, amount);
arr[i].setFinancialAid(a, finaid);
}
}
updateFinancialAid(NUM_RECORDS, arr, percent);
cout << "\nUpdated Financial Aid Records:"
"\n==============================" << endl;
for (int i = 0; i < NUM_RECORDS; ++i)
{
arr[i].display();
cout << endl;
}
}
//-- Insert contents of Figure 1.9 here.
|
Figure 1.11 Header File for the FinancialAidAward Class |
/*- FinancialAid.h ---------------------------------------------------------
Header file of the class library for the class FinancialAid that
models student financial aid records.
-----------------------------------------------------------------------*/
#ifndef FINAIDAWARD
#define FINAIDAWARD
#include <string>
class FinancialAidAward
{
public: // Function members
//-- Constructors
FinancialAidAward();
/*-----------------------------------------------------------------------
Default constructor
Precondition: None.
Postcondition: FinancialAidAward object has been constructed in which
idNumber is 0, name and source are empty strings, and amount is 0.
-----------------------------------------------------------------------*/
FinancialAidAward(string src, double amt);
/*-----------------------------------------------------------------------
Explicit-value constructor
Precondition: id > 0 and amt >= 0
Postcondition: FinancialAidAward object has been constructed with
idNumber = id, sourced = src, amount = amt.
-----------------------------------------------------------------------*/
//-- Accessors
double getAmount() const;
/*-----------------------------------------------------------------------
Postcondition: Value stored in amount is returned.
-----------------------------------------------------------------------*/
string getSource() const;
/*-----------------------------------------------------------------------
Postcondition: Value stored in source is returned.
-----------------------------------------------------------------------*/
//-- Mutators
void setAmount(double newAmount);
/*-----------------------------------------------------------------------
Precondition: newAmount >= 0
Postcondition: amount has been changed to newAmount.
-----------------------------------------------------------------------*/
void setSource(string newSource);
/*-----------------------------------------------------------------------
Precondition: None.
Postcondition: source has been changed to newSource.
-----------------------------------------------------------------------*/
//-- Output
void display() const;
/*-----------------------------------------------------------------------
Precondition: None.
Postcondition: FinancialAid object has been output to cout.
-----------------------------------------------------------------------*/
private: // Data members
string source; // source of financial aid
double amount; // amount of financial aid
}; // end of class declaration
#endif
|
Figure 1.12 Implementation File for the FinancialAidAward Class |
/*- FinancialAid.cpp -------------------------------------------------------
Implementation file of the class library for the class FinancialAid
that models student financial aid records.
-----------------------------------------------------------------------*/
#include <iostream> // cout
#include <string> // string
#include <cassert> // assert
using namespace std;
#include "FinancialAidAward.h"
//-- Default constructor
FinancialAidAward::FinancialAidAward()
: source(""), amount(0)
{ }
//-- Explicit-value constructor
FinancialAidAward::FinancialAidAward(string src, double amt)
{
assert(amt >= 0); // check preconditions
amount = amt;
source = src;
}
//-- Accessors
double FinancialAidAward::getAmount() const
{ return amount; }
string FinancialAidAward::getSource() const
{ return source; }
//-- Mutators
void FinancialAidAward::setAmount(double newAmount)
{
assert(amount >= 0);
amount = newAmount;
}
void FinancialAidAward::setSource(string newSource)
{ source = newSource; }
//-- Output
void FinancialAidAward::display() const
{
cout << source << ": $" << amount;
}
|