Line with a line in program

What does account1.output(cout); in this program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
 //DISPLAY 10.7 Alternative BankAccount Class Implementation
//Demonstrates an alternative implementation of the class BankAccount.
#include <iostream>
#include <cmath>
using namespace std;

//Class for a bank account:
class BankAccount
{
public:
    BankAccount(int dollars, int cents, double rate);
    //Initializes the account balance to $dollars.cents and
    //initializes the interest rate to rate percent.

    BankAccount(int dollars, double rate);
    //Initializes the account balance to $dollars.00 and
    //initializes the interest rate to rate percent.

    BankAccount( );
    //Initializes the account balance to $0.00 and the interest rate to 0.0%.

    void update( );
    //Postcondition: One year of simple interest has been added to the account 
    //balance.

    double get_balance( );
    //Returns the current account balance.

    double get_rate( );
    //Returns the current account interest rate as a percentage.

    void output(ostream& outs);
    //Precondition: If outs is a file output stream, then
    //outs has already been connected to a file.
    //Postcondition: Account balance and interest rate 
    //have been written to the stream outs.
private:
    int dollars_part;
    int cents_part;
    double interest_rate;//expressed as a fraction, for example, 0.057 for 5.7%

    double fraction(double percent);
    //Converts a percentage to a fraction. For example, fraction(50.3) 
    //returns 0.503.

    double percent(double fraction_value);
    //Converts a fraction to a percentage. For example, percent(0.503)
    //returns 50.3.
};

int main( )
{
    BankAccount account1(100, 2.3), account2;

    cout << "account1 initialized as follows:\n";
    account1.output(cout);
    cout << "account2 initialized as follows:\n";
    account2.output(cout);

    account1 = BankAccount(999, 99, 5.5);
    cout << "account1 reset to the following:\n";
    account1.output(cout);
    return 0;
}

BankAccount::BankAccount(int dollars, int cents, double rate)
{
    if ((dollars < 0) || (cents < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
        exit(1);
    }
    dollars_part = dollars;
    cents_part = cents;
    interest_rate = fraction(rate);
}

BankAccount::BankAccount(int dollars, double rate)
{
    if ((dollars < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
        exit(1);
    }
    dollars_part = dollars;
    cents_part = 0;
    interest_rate = fraction(rate);
}

BankAccount::BankAccount( ) : dollars_part(0), cents_part(0), interest_rate(0.0)
{
    //Body intentionally empty.
}


double BankAccount::fraction(double percent_value)
{
    return (percent_value/100.0);
}

//Uses cmath:
void BankAccount::update( )
{
    double balance = get_balance( );
    balance = balance + interest_rate*balance;
    dollars_part = floor(balance);
    cents_part = floor((balance - dollars_part)*100);
}

double BankAccount::get_balance( )
{
    return (dollars_part + 0.01*cents_part);
}

double BankAccount::percent(double fraction_value)
{
    return (fraction_value*100);
}

double BankAccount::get_rate( )
{
    return percent(interest_rate);
}

//Uses iostream:
void BankAccount::output(ostream& outs)
{
    outs.setf(ios::fixed);
    outs.setf(ios::showpoint);
    outs.precision(2);
    outs << "Account balance $" << get_balance( ) << endl;
    outs << "Interest rate " << get_rate( ) << "%" << endl;
}
Topic archived. No new replies allowed.