In: Computer Science
c++
Redefine CDAccount from Display 10.1 so that it is a class rather than a structure. Use the same member variables as in Display 10.1 but make them private. Include member functions for each of the following: one to return the initial balance, one to return the balance at maturity, one to return the interest rate, and one to return the term. Include a constructor that sets all of the member variables to any specified values, as well as a default constructor. Embed your class definition in a test program.
10.1 display
#include <iostream>
3 using namespace std;
4 //Structure for a bank certificate of deposit:
5 struct CDAccount
6 {
7 double balance;
8 double interestRate;
9 int term; //months until maturity
10 };
11
12
13 void getData(CDAccount& theAccount);
14 //Postcondition: theAccount.balance and
theAccount.interestRate
15 //have been given values that the user entered at the
keyboard.
16
17
18 int main( )
19 {
20 CDAccount account;
21 getData(account);
22
23 double rateFraction, interest;
24 rateFraction = account.interestRate / 100.0;
25 interest = account.balance * rateFraction * (account.term /
12.0);
26 account.balance = account.balance + interest;
27
28 cout.setf(ios::fixed);
29 cout.setf(ios::showpoint);
30 cout.precision(2);
31 cout << "When your CD matures in "
32 << account.term << " months,\n"
33 << "it will have a balance of $"
34 << account.balance << endl;
35 return 0;
36 }
37
38 //Uses iostream:
39 void getData(CDAccount& theAccount)
40 {
41 cout << "Enter account balance: $";
42 cin >> theAccount.balance;
43 cout << "Enter account interest rate: ";
44 cin >> theAccount.interestRate;
45 cout << "Enter the number of months until
maturity\n"
46 << "(must be 12 or fewer months): ";
47 cin >> theAccount.term;
48 }
If you have any doubts, please give me comment...
#include <iostream>
using namespace std;
class CDAccount
{
private:
double balance;
double interestRate;
int term; //months until maturity
public:
CDAccount(){
balance = 0;
interestRate = 0.0;
term = 0;
}
CDAccount(double bal, double interest, int _term){
balance = bal;
interestRate = interest;
term = _term;
}
double getInitialBalance(){
return balance;
}
double getBalance(){
double rateFraction, interest;
rateFraction = interestRate / 100.0;
interest = balance * rateFraction * (term / 12.0);
return balance + interest;
}
double getInterestRate(){
return interestRate;
}
int getTerm(){
return term;
}
};
void getData(double &balance, double &interestRate, int &terms);
int main()
{
double balance, interestRate;
int terms;
getData(balance, interestRate, terms);
CDAccount account(balance, interestRate, terms);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "When your CD matures in "
<< account.getTerm() << " months,\n"
<< "it will have a balance of $"
<< account.getBalance() << endl;
return 0;
}
//Uses iostream:
void getData(double &balance, double &interestRate, int &terms)
{
cout << "Enter account balance: $";
cin >> balance;
cout << "Enter account interest rate: ";
cin >> interestRate;
cout << "Enter the number of months until maturity\n"
<< "(must be 12 or fewer months): ";
cin >> terms;
}