Inheritance Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djboyse
    New Member
    • Apr 2007
    • 9

    #1

    Inheritance Problems

    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.

    Code:
    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
    }
    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.
    Last edited by Ganon11; Apr 12 '07, 10:06 PM. Reason: code tags added
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    • djboyse
      New Member
      • Apr 2007
      • 9

      #3
      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..

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        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.

        Comment

        Working...