overload or inherit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • epanda

    #1

    overload or inherit

    Hi,

    I would like to merge two class which have got 90% of same source
    code. But I don't know what to do.

    class A {

    int putLightOn () {

    switch(status)
    {
    case iAmOff :
    print "I am off" ;
    print "put me on!!!\n" ;
    }
    }

    }


    class B {

    int putLightOn () {

    switch(status)
    {
    case iAmOff :
    print "I am off" ;
    print "and I am a red light" ; <<<< the only difference
    between A and B
    print "put me on!!!\n" ;
    }
    }

    }


    I would like that B inherit from A but I don't know how re writing B's
    putLightOn function

    Can you help me?
    Thanks
  • Kevin Frey

    #2
    Re: overload or inherit

    These kinds of questions are hard to give good answers to because the
    classes don't convey any kind of real-world concept.

    It is not a case of "overload" OR inherit - because usually (in a class
    sense) you will perform overloading as a result of inheritance, to change
    part of the derived class' behaviour.

    In you are looking to deal with class A and class B in a polymorphic manner
    (eg. class A is "Light" and class B is a specialised type of light, such as
    "InfraredLight" ), then you will want to make certain methods virtual (the
    ones which change between implementations , as opposed to the "common"
    implementation.

    If on the other hand you are just looking to use inheritance as a way to
    "re-use" some of the implementation, but not necessarily in a polymorphic
    manner, then just derive B from A and overload the methods that need to
    change.

    From my perspective, it looks like you are more looking to do the latter.


    Comment

    • epanda

      #3
      Re: overload or inherit

      Ok thanks Kevin.

      Kevin Frey a écrit :
      These kinds of questions are hard to give good answers to because the
      classes don't convey any kind of real-world concept.
      >
      It is not a case of "overload" OR inherit - because usually (in a class
      sense) you will perform overloading as a result of inheritance, to change
      part of the derived class' behaviour.
      >
      In you are looking to deal with class A and class B in a polymorphic manner
      (eg. class A is "Light" and class B is a specialised type of light, such as
      "InfraredLight" ), then you will want to make certain methods virtual (the
      ones which change between implementations , as opposed to the "common"
      implementation.
      >
      If on the other hand you are just looking to use inheritance as a way to
      "re-use" some of the implementation, but not necessarily in a polymorphic
      manner, then just derive B from A and overload the methods that need to
      change.
      >
      From my perspective, it looks like you are more looking to do the latter.

      Comment

      • James Kanze

        #4
        Re: overload or inherit

        On Aug 5, 10:31 pm, epanda <callingel...@h otmail.frwrote:
        I would like to merge two class which have got 90% of same
        source code. But I don't know what to do.
        class A {
        int putLightOn () {
        switch(status)
        {
        case iAmOff :
        print "I am off" ;
        print "put me on!!!\n" ;
        }
        }
        }
        class B {
        int putLightOn () {
        switch(status)
        {
        case iAmOff :
        print "I am off" ;
        print "and I am a red light" ; <<<< the only difference
        between A and B
        print "put me on!!!\n" ;
        }
        }
        }
        I would like that B inherit from A but I don't know how re
        writing B's putLightOn function
        As Kevin said, it largely depends on whether you want the two
        classes to behave polymorphically nor not. If so, look into the
        Template Method pattern or the Strategy pattern. If not,
        consider using variations on mixins.

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...