I'm kind of new to C++ and having some problems with inheritance. I've tried looking everywhere for some help, but I'm not too sure what to ask so please any input will be greatly appreciated. (Please let me no if I start talking rubbish too)
Basically I have my base class with 3 functions and two global integers. I also have a derived class which overrides two of the base class functions.
When I create the derived class object, I want it to set the first integer, then run the base class function that isn't overridden, which will set the second integer based on the first integer, then the other overridden function will be able to use both the integers' new values. I'm not too good at explaining so ill try demonstrate.
Expand|Select|Wrap|Line Numbers
- class base
- {
- int nA = 0, nB = 0;
- funcA(void);
- funcB(void);
- funcC(void);
- };
- base::funcA() { return nA + nB; }
- base::funcB() { nA = 1; }
- base::funcC() {nB = 1; }
- class derived
- {
- funcB(void);
- funcC(void);
- };
- derived::funcB() { nA = 5; nB = 5; }
- derived::funcC() { return funcA() * 2; }
- int main()
- {
- derived dClass; //create object
- dClass.funcB(); //run function to set integers
- cout << dClass.funcC(); //do the sums and output
- }
I think my problem is that the base class will try do 0+0.
If no one can help ill try rephrase it, cheers any ways though.