By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Inheritance Problems

P: 9
Hey,

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
  1. class base
  2. {
  3.    int nA = 0, nB = 0;
  4.    funcA(void);
  5.    funcB(void);
  6.    funcC(void);
  7. };
  8.  
  9. base::funcA() { return nA + nB; }
  10. base::funcB() { nA = 1; }
  11. base::funcC() {nB = 1; }
  12.  
  13. class derived
  14. {
  15.    funcB(void);
  16.    funcC(void);
  17. };
  18.  
  19. derived::funcB() { nA = 5; nB = 5; }
  20. derived::funcC() { return funcA() * 2; }
  21.  
  22. int main()
  23. {
  24.    derived dClass;                //create object
  25.    dClass.funcB();                //run function to set integers
  26.    cout << dClass.funcC();    //do the sums and output
  27. }
Basically will this output 20? if not, how can I get it to?

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.
Apr 12 '07 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
As it stands, I'm pretty sure this program won't compile. The integers nA and nB are not given access specifiers in base - that is, you don't explicitly say whether they are private, public, or protected. When you don't explicitly say public, etc. it is assumed that the members are private (when using a class). That means that only base can see nA and nB - derived knows nothing about them. When you type nA = 5; nB = 5; in derived::funcB(), you will run into errors.

I don't know how accurate this example is to your actual problem, but this could be something that will affect it. Also, you should look up virtual functions for overriding base class functions - it may not be necessary, but the learning is worth it.
Apr 12 '07 #2

P: 9
Thanks for the quick reply. They are all public. I basically want an integer specified in the base class that both classes can access. So like a function in the base class might set it to 1, then a function in the derived class may set it to 5, then a function in the base class may read it, perform a calculation and set it to 3 etc..
Apr 12 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
If they are all public, then I can't think of any problem you will have. Since a derived is-a base, it will have its own nA and nB (or maybe just have access to base::nA and base::nB). You may want to make nA and nB protected, however - it still lets derived access and change them, but it prevents other classes from accessing them directly, too.
Apr 12 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.