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.
  • 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
                        • M Mark Salsbery

                          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 Offline
                          F Offline
                          ForNow
                          wrote on last edited by
                          #21

                          Thankx I think I am ready to code.... After being a Dinasour Assembler programmer for 30 years writting procedurel code its hard to change my way thinking

                          M 1 Reply Last reply
                          0
                          • F ForNow

                            Thankx I think I am ready to code.... After being a Dinasour Assembler programmer for 30 years writting procedurel code its hard to change my way thinking

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

                            Have fun!   I coded a few years in assembler and C before C++, but not 30 :) Thinking in terms of classes will be second-nature in no time. Cheers, MArk

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

                            F 1 Reply Last reply
                            0
                            • M Mark Salsbery

                              Have fun!   I coded a few years in assembler and C before C++, but not 30 :) Thinking in terms of classes will be second-nature in no time. Cheers, MArk

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

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

                              Thankx

                              1 Reply Last reply
                              0
                              • L led mike

                                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 Offline
                                1 Offline
                                1slipperyfish
                                wrote on last edited by
                                #24

                                i thought that was what was happening and wondered why use A and B?? i am only a learner myself:-O i am on leave all week so any day suits me:-D paul

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

                                1 Reply 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

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

                                  Puncuation of some sort would extremely help in getting your ideas across.


                                  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
                                  • J jhwurmbach

                                    Puncuation of some sort would extremely help in getting your ideas across.


                                    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
                                    #26

                                    ok thankx

                                    1 1 Reply Last reply
                                    0
                                    • F ForNow

                                      ok thankx

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

                                      can i add a quetion on the end? i have a base class animalClass.hpp i have a derived Dog class and main in dogObj.cpp and a derived Cat class in catObj.cpp what i wanted to know is should i have animalClass.hpp dogClass.hpp, catClass.hpp, declaring the classes and have a seperate animalObj.cpp for main() to constrol the functions etc? at the moment my animalClass is about fifty lines as are the other animal classes, with my main () about 80 lines thanks in advance paul

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

                                      L F 2 Replies Last reply
                                      0
                                      • 1 1slipperyfish

                                        can i add a quetion on the end? i have a base class animalClass.hpp i have a derived Dog class and main in dogObj.cpp and a derived Cat class in catObj.cpp what i wanted to know is should i have animalClass.hpp dogClass.hpp, catClass.hpp, declaring the classes and have a seperate animalObj.cpp for main() to constrol the functions etc? at the moment my animalClass is about fifty lines as are the other animal classes, with my main () about 80 lines thanks in advance paul

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

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

                                        1slipperyfish wrote:

                                        should i have animalClass.hpp dogClass.hpp, catClass.hpp, declaring the classes and have a seperate animalObj.cpp

                                        you need header files (.h, .hpp) if you want to use the classes in other .cpp files so they can be #include < ...> If the class is defined in a cpp file the visibility of it is limited to (not sure) that file maybe.

                                        1 1 Reply Last reply
                                        0
                                        • L led mike

                                          1slipperyfish wrote:

                                          should i have animalClass.hpp dogClass.hpp, catClass.hpp, declaring the classes and have a seperate animalObj.cpp

                                          you need header files (.h, .hpp) if you want to use the classes in other .cpp files so they can be #include < ...> If the class is defined in a cpp file the visibility of it is limited to (not sure) that file maybe.

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

                                          thanks for the reply led mike :D i have rewritten my classes into baseClass.hpp, CatClass.hpp, and DogClass.hpp with farms.cpp initialising the functions and having main() i had a lot of trouble compiling as it kept saying i had already declared my Mammal class in baseClass however if you add [code]#ifndef BASE_CLASS_HPP #define BASE_CLASS_HPP[/code] at the top of baseClass.hpp and [code]#endif[/code] at the bottom it works. you all probably already new that but i've just wasted 2 1/2 hours of my life learning that the hard way:D thanks again paul

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

                                          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