Extending Classes: Overriding methods

I have two classes.
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
class Account   {
    protected:
        string* owner;
        int accNum;
        double value;
        double tax;
        double inflation;

    public:
        Account(string, double, double, double);
        ~ Account();
        string* toString();
        void updateValue(double);
        bool isValue(double);
        double getValue();
    private:
        string* getCurrency();
};

class Bond : public Account {
    private:
        double intRate;
    public:
        Bond(string, double, double, double);
        void updateValue(double);
};

I'm trying to override the method updateValue in account at the bond level but if i do this:
1
2
3
vector<Account> accounts;
accounts.push_back(Bond("Bond", 1000, 0.1, 0.15))
accounts.front().updateValue(4.5);


The call in line 3 will just call the Account method, and that doesn't really do anything. How do i get this to work as I expect?

Lastly here's my Bond::updateValue(double) method:
1
2
3
4
5
6
7
8
9
10
11
12
13
void Bond::updateValue(double yearsIn)   {
    while(yearsIn > 0)  {
        double i = value * intRate;
        double pen = (value + i) * inflation;
        double gain = i - pen;
        if(yearsIn < 1)   {
            gain *= yearsIn;
        }
        value += gain;
        yearsIn--;
    }
    return;
}
You should probably make the function you want to override a virtual function like this:

virtual void updateValue(double);

This is explained in more detail on this page:

http://www.cplusplus.com/doc/tutorial/polymorphism/
closed account (DSLq5Di1)
Will need to access your object through a pointer or reference too.
Topic archived. No new replies allowed.