Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Design question for using derived class/ Multiple Base classes

Design question for using derived class/ Multiple Base classes

Scheduled Pinned Locked Moved C / C++ / MFC
questiondesign
31 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    I have a basic OOP question I have a Base class lets I'll call it B I am going to add functionality to this in addition I will need to use multiple copies of the Base class My question is What is the best programming approach CLass A : B { OR CLass A { B mybclass[10]; Thankx in advance

    J L 2 Replies Last reply
    0
    • F ForNow

      I have a basic OOP question I have a Base class lets I'll call it B I am going to add functionality to this in addition I will need to use multiple copies of the Base class My question is What is the best programming approach CLass A : B { OR CLass A { B mybclass[10]; Thankx in advance

      J Offline
      J Offline
      jhwurmbach
      wrote on last edited by
      #2

      ForNow wrote:

      What is the best programming approach

      No one can say that once and for all. The classical oO-analysis question would be: IS every A also a B in every respect? Or does it HAVE a B, that is, does it use a B to do its work.


      Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
      George Orwell, "Keep the Aspidistra Flying", Opening words

      F 1 Reply Last reply
      0
      • F ForNow

        I have a basic OOP question I have a Base class lets I'll call it B I am going to add functionality to this in addition I will need to use multiple copies of the Base class My question is What is the best programming approach CLass A : B { OR CLass A { B mybclass[10]; Thankx in advance

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        ForNow wrote:

        My question is What is the best programming approach

        The difference requires context that you have not supplied. Your question as posted is not possible to answer.

        1 F 2 Replies Last reply
        0
        • L led mike

          ForNow wrote:

          My question is What is the best programming approach

          The difference requires context that you have not supplied. Your question as posted is not possible to answer.

          1 Offline
          1 Offline
          1slipperyfish
          wrote on last edited by
          #4

          i don't understand what the question is?? if you have an Animal base class and you want to derive a Dog from it surely you would put class Dog : public Animal { }; or is that not the question you're asking? i am only a learner myself but if you're asking is it right or wrong, no there is no answer whichever you're most comfortable with hth paul

          if ignorance is bliss then knock the smile off my face!!!

          L 1 Reply Last reply
          0
          • 1 1slipperyfish

            i don't understand what the question is?? if you have an Animal base class and you want to derive a Dog from it surely you would put class Dog : public Animal { }; or is that not the question you're asking? i am only a learner myself but if you're asking is it right or wrong, no there is no answer whichever you're most comfortable with hth paul

            if ignorance is bliss then knock the smile off my face!!!

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            1slipperyfish wrote:

            but if you're asking

            I didn't ask anything so I have no idea what your post is supposed to mean for me.

            1slipperyfish wrote:

            no there is no answer whichever you're most comfortable with

            If that is directed at the original question, "To inherit or to aggregate", it is not a valid answer.

            1 M 2 Replies Last reply
            0
            • L led mike

              1slipperyfish wrote:

              but if you're asking

              I didn't ask anything so I have no idea what your post is supposed to mean for me.

              1slipperyfish wrote:

              no there is no answer whichever you're most comfortable with

              If that is directed at the original question, "To inherit or to aggregate", it is not a valid answer.

              1 Offline
              1 Offline
              1slipperyfish
              wrote on last edited by
              #6

              sorry led mike i thought you just posted and it joined the rest i wasn't posting to you :confused: why wouldn't you inherit? paul

              if ignorance is bliss then knock the smile off my face!!!

              L 1 Reply Last reply
              0
              • L led mike

                1slipperyfish wrote:

                but if you're asking

                I didn't ask anything so I have no idea what your post is supposed to mean for me.

                1slipperyfish wrote:

                no there is no answer whichever you're most comfortable with

                If that is directed at the original question, "To inherit or to aggregate", it is not a valid answer.

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                led mike wrote:

                I didn't ask anything so I have no idea what your post is supposed to mean for me.

                I chuckled at the irony (or is it coincidence) in the user name :)

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                L 1 Reply Last reply
                0
                • M Mark Salsbery

                  led mike wrote:

                  I didn't ask anything so I have no idea what your post is supposed to mean for me.

                  I chuckled at the irony (or is it coincidence) in the user name :)

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  :laugh::laugh: I had to read your post like 4 times to figure that out! :laugh::laugh:

                  1 Reply Last reply
                  0
                  • 1 1slipperyfish

                    sorry led mike i thought you just posted and it joined the rest i wasn't posting to you :confused: why wouldn't you inherit? paul

                    if ignorance is bliss then knock the smile off my face!!!

                    L Offline
                    L Offline
                    led mike
                    wrote on last edited by
                    #9

                    1slipperyfish wrote:

                    why wouldn't you inherit?

                    You are just not following the thread. Your post used classes "Animal" and "Dog" which provide "context" and obviously a Dog "is a" Animal thereby indicating inheritance. However, the original poster used the classes "A" and "B" which provide no context and therefore we cannot distinguish between "is a" or "has a". By the way Mark wants to have lunch with you.

                    1 1 Reply Last reply
                    0
                    • J jhwurmbach

                      ForNow wrote:

                      What is the best programming approach

                      No one can say that once and for all. The classical oO-analysis question would be: IS every A also a B in every respect? Or does it HAVE a B, that is, does it use a B to do its work.


                      Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                      George Orwell, "Keep the Aspidistra Flying", Opening words

                      F Offline
                      F Offline
                      ForNow
                      wrote on last edited by
                      #10

                      The Latter is true A is B plus a whole lot more However in addition to that A is multiple occurences of B Thankx

                      1 Reply Last reply
                      0
                      • L led mike

                        ForNow wrote:

                        My question is What is the best programming approach

                        The difference requires context that you have not supplied. Your question as posted is not possible to answer.

                        F Offline
                        F Offline
                        ForNow
                        wrote on last edited by
                        #11

                        The best exmaple I could give if B is car then A is a fleet of cars with all the options

                        M 1 Reply Last reply
                        0
                        • F ForNow

                          The best exmaple I could give if B is car then A is a fleet of cars with all the options

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #12

                          In that case, your second example (which is neither derived or multiple base classes). 'A' has a fleet (array) of cars but it is not a car. Mark

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          F 1 Reply Last reply
                          0
                          • M Mark Salsbery

                            In that case, your second example (which is neither derived or multiple base classes). 'A' has a fleet (array) of cars but it is not a car. Mark

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            F Offline
                            F Offline
                            ForNow
                            wrote on last edited by
                            #13

                            okay thankx its hard to get into OO thinking

                            M 1 Reply Last reply
                            0
                            • F ForNow

                              okay thankx its hard to get into OO thinking

                              M Offline
                              M Offline
                              Mark Salsbery
                              wrote on last edited by
                              #14

                              If it helps any: "Is ..."     Use inheritance "Has ..."  Add a member Example: given a base class fruit class apple - apple "is" a fruit so derive apple from fruit class fruitbasket - a fruit basket "has" fruit so add fruit (or an array of fruit) as a member of the fruitbasket class Mark

                              Mark Salsbery Microsoft MVP - Visual C++ :java:

                              F 1 Reply Last reply
                              0
                              • M Mark Salsbery

                                If it helps any: "Is ..."     Use inheritance "Has ..."  Add a member Example: given a base class fruit class apple - apple "is" a fruit so derive apple from fruit class fruitbasket - a fruit basket "has" fruit so add fruit (or an array of fruit) as a member of the fruitbasket class Mark

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                F Offline
                                F Offline
                                ForNow
                                wrote on last edited by
                                #15

                                I think I was looking at the other way ....a base has lets say 3 data members/methods.... If I want to use those but I need another method/dats member I will dervie the base and in the inherted class and add to it I think what you are saying it works the other way around the base has 10 data members/methods The drevid class only uses 7 data members/methods

                                M 1 Reply Last reply
                                0
                                • F ForNow

                                  I think I was looking at the other way ....a base has lets say 3 data members/methods.... If I want to use those but I need another method/dats member I will dervie the base and in the inherted class and add to it I think what you are saying it works the other way around the base has 10 data members/methods The drevid class only uses 7 data members/methods

                                  M Offline
                                  M Offline
                                  Mark Salsbery
                                  wrote on last edited by
                                  #16

                                  Data members/methods and the amount of data members/methods isn't relevant to the class hierarchy. Those are only relevant to the class they're members of.

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                                  F 1 Reply Last reply
                                  0
                                  • M Mark Salsbery

                                    Data members/methods and the amount of data members/methods isn't relevant to the class hierarchy. Those are only relevant to the class they're members of.

                                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                                    F Offline
                                    F Offline
                                    ForNow
                                    wrote on last edited by
                                    #17

                                    I'll put another way I thought of the concept derived class(s) as a dervived class uses the base class .. but adds functionality Or Is it just the opposite way of thinking a base has all the functionality but derived class just picks the one it needs

                                    M 1 Reply Last reply
                                    0
                                    • F ForNow

                                      I'll put another way I thought of the concept derived class(s) as a dervived class uses the base class .. but adds functionality Or Is it just the opposite way of thinking a base has all the functionality but derived class just picks the one it needs

                                      M Offline
                                      M Offline
                                      Mark Salsbery
                                      wrote on last edited by
                                      #18

                                      ForNow wrote:

                                      a dervived class uses the base class .. but adds functionality

                                      A derived class IS the base class.  The derived class can alter and/or add to the functionality of its base class. Again, the class hierarchy shouldn't be designed around the members.  The members are whatever is necessary to implement the functionality of a class. Mark

                                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                                      F 1 Reply Last reply
                                      0
                                      • M Mark Salsbery

                                        ForNow wrote:

                                        a dervived class uses the base class .. but adds functionality

                                        A derived class IS the base class.  The derived class can alter and/or add to the functionality of its base class. Again, the class hierarchy shouldn't be designed around the members.  The members are whatever is necessary to implement the functionality of a class. Mark

                                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                                        F Offline
                                        F Offline
                                        ForNow
                                        wrote on last edited by
                                        #19

                                        I understand I am going say something to see if I undertand what you mean a typical way of altering functionality would be to declare a method as Virtual that way the derived class can implement Its version of the method Or Since dervied members can have thier version of the Base('s) constructer that would be a way of altering the functionality

                                        M J 2 Replies Last reply
                                        0
                                        • F ForNow

                                          I understand I am going say something to see if I undertand what you mean a typical way of altering functionality would be to declare a method as Virtual that way the derived class can implement Its version of the method Or Since dervied members can have thier version of the Base('s) constructer that would be a way of altering the functionality

                                          M Offline
                                          M Offline
                                          Mark Salsbery
                                          wrote on last edited by
                                          #20

                                          ForNow wrote:

                                          a typical way of altering functionality would be to declare a method as Virtual that way the derived class can implement Its version of the method

                                          Yes.  That's polymorphism (through inheritance)

                                          ForNow wrote:

                                          Since dervied members can have thier version of the Base('s) constructer that would be a way of altering the functionality

                                          Yes.  Derived class constructors can also both alter and extend the base class constructor implementation, but it's not quite the same as overriding a virtual function.  There's nothing virtual about constructors.

                                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                                          F 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups