Bank Account Homework

Hello,

I was assigned this homework.
Create a class BankAccount. The class contains balance(non negative double),
rate(non negative double) and account_id which is a 12 digit number.
1)Create a default constructor(balance=0,rate=0, account_id=111111111111)
2)Create a constructor that accepts parameters for all the features of the class.
3)Provide class with get_ methods for feature access to the class.
4)Provide class with set_ methods for feature change to the class.
4)Provide a update() method that adds an 1 year interest to the balance.

Here is my code so far.

UPDATE 23.3.2016
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
#include <iostream>
#include <stdexcept>

class BankAccount {
    private:
        double balance;
        double rate;
        long long account_id;
    public:
//default constructor
        BankAccount(double b=0, double r=0, long long a=111111111111) {
            set_balance(b);
            set_rate(r);
            set_account_id(a);
        }
        double get_balance() const{
            return balance;
            }
        void set_balance(double x){
            if (x < 0) {
                throw std::runtime_error("balance must be non-negative");
            }
            balance = x;
        }
        double get_rate() const{ return rate; }
        void set_rate(double x){
            if (x < 0) {
                throw std::runtime_error("rate must be non-negative");
            }
            rate = x;
        }
        long long get_account_id() const{
             return account_id;
             }
        void set_account_id(long long x){
            if (x < 111111111111 || 1000000000000 <= x) {
                throw std::runtime_error("account must be 12 digits");
            }
            account_id = x;
        }
//update function
        void update() {
            set_balance(balance + balance * rate);
        }
};

int main() {
    try {
        BankAccount account(-1, 5.5, 123456789012);
        std::cout << account.get_balance() << std::endl;
        std::cout << account.get_rate() << std::endl;
        std::cout << account.get_account_id() << std::endl;
        account.update();
        std::cout << account.get_balance() << std::endl;
    } catch (const std::runtime_error& e) {
        std::cout << e.what() << std::endl;
        return 1;
    }
    return 0;
}


My teacher says me it's completely wrong but he doesn't tell me what's wrong.
Please help. :(
CS Student here.
Last edited on
What kind of output do you get for:

1
2
3
4
5
6
int main() {
    BankAccount account(100, 5.5, 123456789);
    cout << account.get_rate() << '\n';
    cout << account.get_account_id() << '\n';
    cout << account.get_balance() << '\n';
}
Hello,

The code executes without any problem but the output is weird.


6.9531e-308
2.84618e+018
6.95207e-308
Last edited on
And, can you determine why that is by inspecting your code?
The update method is wrong, i am sure.
I cannot see any other problem.
I am confused.
Line 31-32: account_id is declared as a long long, but you're passing a double.

Line 35-36: account_id is declared as a long long, but you're returning a double.

Line 79: You're calling set_account_id which is expecting a double, but you're passing a long long.

Pay attention to the warnings generated by the compiler.

Line 52: What is the purpose of the update member variable?

Line 73: Your while statement allows a value of 1000000000000 (13 digits). The condition should be >=.

line 19, 27, 35: Your getters are not const correct.

Line 43: Why is your getter modifying the balance? getters should not modify the object.

Line 80: This line sets the balance (see 39-40). You've already done that. I don't think that is what you meant to do here.


Hello AbstractionAnon,

Your points are totally correct.
I updated my code.
But it seems i get weird outputs again.
I cannot get to work this:
4)Provide a update() method that adds an 1 year interest to the balance.
Any ideas?
I really appreciate your help. :)
The update method is wrong, i am sure

The update method wasn't called in the code I tendered.
Cire,

Even if i try to add
 
cout << account.get_update() << '\n'; 

The result is 0.

So the update function is wrong.
Also we miss something important.
The balance and rate values have to be non negative.
Any suggestions?
Last edited on
Cire,

Even if i try to add

I think you miss the point. Your object contains junk prior to and without calling get_update. Calling get_update while your object contains junk is clearly not going to be productive. I'm sure you've heard the phrase "garbage in, garbage out."
Cire,

Your point is right. It's not the update method that's causing the problem.
I updated my code and it looks fine to me.
I literally don't know where the problem is. :(
Dafgod,

Take a look at lines 12 - 13 of your updated code snippet:

1
2
 BankAccount(double balance,double rate,long long account_id){
        }


This is the definition of the constructor you call on line 55. As you can see, the constructor isn't actually doing anything, and the member variables remain unchanged from their initial garbage state. Since the member variables aren't initialized, any get-methods accessing them will return garbage.

You'll need to do one of the following:

1
2
3
4
5
6
7
BankAccount(double balance, double rate, long long account_id) {
		
		this->balance = balance;
		this->rate = rate;
		this->account_id = account_id;

	}


Make use of the this pointer. Since you've chosen to give the exact same names of the member variables to the arguments of the constructor, you'll have to be explicit.

Or, you could just change the names to avoid the ambiguity:
1
2
3
4
5
6
7
BankAccount(double _balance, double _rate, long long _account_id) {

		balance = _balance;
		rate = _rate;
		account_id = _account_id;

	}


Or, even better, use an initializer list:

BankAccount(double _balance, double _rate, long long _account_id) : balance(_balance), rate(_rate), account_id(_account_id) {}
Last edited on
Xismn,

Thank you so much!! You are a lifesaver.
I didn't notice these minor mistakes.
Now my program works.
The only problem is that i cannot get the update function to work.
Any ideas?
Hi dafgod,

in your default constructor, where do you assign the values that you pass creating a new instance to the members of this new instance?

1
2
3
4
5
//somewhere here below there are some assignments missing...
        BankAccount(double balance,double rate,long long account_id){
        }
        ⁞


answer is in here: http://www.cplusplus.com/doc/tutorial/classes/

[quote]
.... For example, consider a class with the following declaration:

1
2
3
4
5
6
class Rectangle {
    int width,height;
  public:
    Rectangle(int,int);
    int area() {return width*height;}
};

The constructor for this class could be defined, as usual, as:
Rectangle::Rectangle (int x, int y) { width=x; height=y; }

But it could also be defined using member initialization as:
Rectangle::Rectangle (int x, int y) : width(x) { height=y; }

Or even:
Rectangle::Rectangle (int x, int y) : width(x), height(y) { }

Note how in this last case, the constructor does nothing else than initialize its members, hence it has an empty function body....
[\quote]

I think if you understand that part, you will get something else than garbage out

HTH
RobiBue,

Thank you for your suggestions.
I updated my code.
All getters work fine except update. :(
Last edited on
Dafgod,

Your update-getter isn't working for the same reason that your other getters didn't work before: You didn't initialize the members that the get-methods were accessing.

One look at your constructor(s) reveals that the member variable update was never initialized, and contains garbage.

In addition, and this is unrelated, your set_update() method doesn't actually "set" update to anything - it just returns what should have presumably been assigned to update, and then returned.
Last edited on
Allright,

The code is updated!
I also put restrictions(non negative balance and rate).
Also i created an update funtion.

If you run the code you can see that if balance is negative it displays the warning message and the value. Also the update function outputs some weird value.
Last edited on
If you run the code you can see that if balance is negative it displays the warning message and the value.


Did you want it not to? It's doing that because the message is printed in the constructor, but later on the get_balance() method is invoked on line 69.

Also the update function outputs some weird value.


That's because the update() function is doing math with the member variable balance. The balance never gets initialized if the _balance passed into the constructor is less than zero. If you do anything with uninitialized garbage variables, you just make more garbage.

Also, your update function's signature indicates that it returns a double, but it doesn't actually return anything.
Last edited on
After studying extensively, i just updated my code.
What do you think?
Looks much better.

Line 49: This constructor is going to throw due to the negative balance argument. You will never get to lines 50-54.



Topic archived. No new replies allowed.