Base classes

Discussion in 'C' started by sharanbr, Aug 20, 2010.

  1. sharanbr

    sharanbr New Member

    Joined:
    Aug 20, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Folks,

    I have a question on what should be the approach while designing a base class.
    My idea of a base class (until few days back) was to have as much code packed in it
    and then have minimal changes in the derived class. This way, the coding work in
    derived classes is less. But looking at some of the books on writing reusable code,
    I think this is not the correct method.

    Can someone give direction or point to article that discusses this?

    Regards,
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Any code that's common to all classes would be best placed in the base class.
    Any code that's specific to a derived class would be best placed in that derived class.
    No derived class code should be present in the base class, and similarly no base class code should be present in the derived classes.
     
    sharanbr likes this.
  3. sharanbr

    sharanbr New Member

    Joined:
    Aug 20, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Thank you. Apart from what you said, it is also recommended that base class make no direct reference to concrete class even though such function might not belong to derived class. This way the base class does not have to change with every change to derived class.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice