Help with classes, objects, and the correct output

I have been asked to change this program from a struct to a class with all private variables and some new member functions. What I have so far does compile and run but not correctly. One thing i am having trouble with is how do i let the user input values that are private variables??? Im not to sure where to go from here at all, any help would be super awesome. Thank you

on a side note how do i get the line numbers to copy and paste with my code?

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
#include <iostream>
using namespace std;


//class for a bank certificate of deposit:
class CDAccount
{   
private:
    double balance;
    double interest_rate;
    int term;//months until maturity
    
public:
       CDAccount(); //default constructor (works)
       
       CDAccount (double & balance, double & interest_rate, int & term); //constructor w/ arguments
       
       void display()const; //display the CD Account (works)
       
       double Balance (double b); //return the initial balance (works) 
      
       double IntrestRate (double ir); //return interest rate (works)
       
       int Term (int t); //return term in months (works)
       
       double MatureBalance (double InitialBalance, double IntrestRate, int term); //return balance at maturity
       
       //void get_data(double balance, double intrest_rate, int term);
//Postcondition: the balance, the interest_rate and the term 
//have been given values that the user entered at the keyboard.
};

CDAccount::CDAccount(): balance(0), interest_rate(0), term(0)
{}//default 

CDAccount::CDAccount (double & balance, double & interest_rate, int & term) :
    balance(balance), interest_rate(interest_rate), term(term)
{}//constructor 

void CDAccount::display() const 
{
     cout << "the ballance is: " << balance << " the interest rate is: " << interest_rate << " the term in months is: " << term << endl;
}

double CDAccount::Balance (double b)
{
    cout << "please enter the account balance" << endl;
     cin >> b; 
     balance=b;
}

double CDAccount::IntrestRate (double ir)
{
       cout << "please enter the intrest rate as a decimal" << endl;
       cin >> ir;
       interest_rate=ir;
}

int CDAccount::Term(int t)
{
    cout << "please enter the term lenght in months" << endl;
    cin >> t;
    term=t;
}
int main( )
{
    CDAccount account;
    account.display(); //display the default constructor 
    account.Balance(0);
    account.IntrestRate(0);
    account.Term(0);
    account.display();


   // double rate_fraction, interest;
    //rate_fraction = account.interest_rate/100.0;
   // interest = account.balance*rate_fraction*(account.term/12.0);
   // account.balance = account.balance + interest;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "When your CD matures in " 
         << account.Term(0) << " months,\n"
        << "it will have a balance of $" 
         << account.Balance(0) << endl;
    system("pause");
    return 0;
}

//Uses iostream:
 //void get_data(double balance, double intrest_rate, int term)
//{
   //cout << "Enter account balance: $";
   //cin >> get_data.balance;

    //cout << "Enter account interest rate: ";
    //cin >> the_account.interest_rate;
    //cout << "Enter the number of months until maturity\n"
         //<< "(must be 12 or fewer months): ";
    //cin >> the_account.term;
//}
Last edited on
off to the side you see format buttons. use the first and paste your code inbetween. Like this...

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
#include <iostream>
using namespace std;


//class for a bank certificate of deposit:
class CDAccount
{ 
private:
double balance;
double interest_rate;
int term;//months until maturity

public:
CDAccount(); //default constructor 

CDAccount (double & balance, double & interest_rate, int & term); //constructor w/ arguments

void display()const; //display the CD Account 

double Balance (double b); //return the initial balance 

double IntrestRate (double ir); //return interest rate 

int Term (int t); //return term in months 

double MatureBalance (double InitialBalance, double IntrestRate, int term); //return balance at maturity

//void get_data(double balance, double intrest_rate, int term);
//Postcondition: the balance, the interest_rate and the term 
//have been given values that the user entered at the keyboard.
};

CDAccount::CDAccount(): balance(0), interest_rate(0), term(0)
{}//default 

CDAccount::CDAccount (double & balance, double & interest_rate, int & term) :
balance(balance), interest_rate(interest_rate), term(term)
{}//constructor 

void CDAccount::display() const 
{
cout << "the ballance is: " << balance << " the interest rate is: " << interest_rate << " the term in months is: " << term << endl;
}

double CDAccount::Balance (double b)
{
cout << "please enter the account balance" << endl;
cin >> b; 
balance=b;
}

double CDAccount::IntrestRate (double ir)
{
cout << "please enter the intrest rate as a decimal" << endl;
cin >> ir;
interest_rate=ir;
}

int CDAccount::Term(int t)
{
cout << "please enter the term lenght in months" << endl;
cin >> t;
term=t;
}
int main( )
{
CDAccount account;
account.display(); //display the default constructor 
account.Balance(0);
account.IntrestRate(0);
account.Term(0);
account.display();


// double rate_fraction, interest;
//rate_fraction = account.interest_rate/100.0;
// interest = account.balance*rate_fraction*(account.term/12.0);
// account.balance = account.balance + interest;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "When your CD matures in " 
<< account.Term(0) << " months,\n"
<< "it will have a balance of $" 
<< account.Balance(0) << endl;
system("pause");
return 0;
}

//Uses iostream:
//void get_data(double balance, double intrest_rate, int term)
//{
//cout << "Enter account balance: $";
//cin >> get_data.balance;

//cout << "Enter account interest rate: ";
//cin >> the_account.interest_rate;
//cout << "Enter the number of months until maturity\n"
//<< "(must be 12 or fewer months): ";
//cin >> the_account.term;
//}
thank you for that
Topic archived. No new replies allowed.