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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Classes and Inheritance, question inside.

Classes and Inheritance, question inside.

Scheduled Pinned Locked Moved C / C++ / MFC
questionooplearning
7 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.
  • R Offline
    R Offline
    rbwest86
    wrote on last edited by
    #1

    Hello everyone, Just so its known this question is related to my programming class. Here is the question out of the book: "Is it reasonable to create a CHawk by deriving from CBird? How about a COstrich? Justify your answers. Derive an avian hierarchy that can cope with both of these birds."

    class CBird
    {
    protected:
    int wingSpan;
    int eggSize;
    int airSpeed;
    int altitude;
    public:
    virtual void fly() { altitude = 100; }
    };

    I thought that the class has to be declared first. Like #include CBird.h goes in the include list. If I am wrong, then the question from the book is YES, CHawk and COstrich can be inherited from CBird. Can anyone provide me with clarification to classes related to OOP? Thanks in advance. V/R Rob

    S D B 3 Replies Last reply
    0
    • R rbwest86

      Hello everyone, Just so its known this question is related to my programming class. Here is the question out of the book: "Is it reasonable to create a CHawk by deriving from CBird? How about a COstrich? Justify your answers. Derive an avian hierarchy that can cope with both of these birds."

      class CBird
      {
      protected:
      int wingSpan;
      int eggSize;
      int airSpeed;
      int altitude;
      public:
      virtual void fly() { altitude = 100; }
      };

      I thought that the class has to be declared first. Like #include CBird.h goes in the include list. If I am wrong, then the question from the book is YES, CHawk and COstrich can be inherited from CBird. Can anyone provide me with clarification to classes related to OOP? Thanks in advance. V/R Rob

      S Offline
      S Offline
      Saurabh Garg
      wrote on last edited by
      #2

      I think the question is trying to check if you understand when you can inherit from a class. I will only provide you a hint - Hawk can fly but Ostrich can't. -Saurabh

      R 1 Reply Last reply
      0
      • S Saurabh Garg

        I think the question is trying to check if you understand when you can inherit from a class. I will only provide you a hint - Hawk can fly but Ostrich can't. -Saurabh

        R Offline
        R Offline
        rbwest86
        wrote on last edited by
        #3

        Thank you very much for the prompt response. So I take it that the non-flying birds will not and can not inherit CBird class. So there would have to be another class to define CNonbird correct?

        T S 2 Replies Last reply
        0
        • R rbwest86

          Thank you very much for the prompt response. So I take it that the non-flying birds will not and can not inherit CBird class. So there would have to be another class to define CNonbird correct?

          T Offline
          T Offline
          Tim Craig
          wrote on last edited by
          #4

          Guess you don't know much about biology either. :laugh: To see if something should fit in a class hierarchy, does it have an "is-a" relationship?

          You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

          1 Reply Last reply
          0
          • R rbwest86

            Thank you very much for the prompt response. So I take it that the non-flying birds will not and can not inherit CBird class. So there would have to be another class to define CNonbird correct?

            S Offline
            S Offline
            Saurabh Garg
            wrote on last edited by
            #5

            As Tim said you should check for is-a relationship. Ostrich *is a* bird but it cannot fly, this suggests that you need to think about how can you organize birds. -Saurabh

            1 Reply Last reply
            0
            • R rbwest86

              Hello everyone, Just so its known this question is related to my programming class. Here is the question out of the book: "Is it reasonable to create a CHawk by deriving from CBird? How about a COstrich? Justify your answers. Derive an avian hierarchy that can cope with both of these birds."

              class CBird
              {
              protected:
              int wingSpan;
              int eggSize;
              int airSpeed;
              int altitude;
              public:
              virtual void fly() { altitude = 100; }
              };

              I thought that the class has to be declared first. Like #include CBird.h goes in the include list. If I am wrong, then the question from the book is YES, CHawk and COstrich can be inherited from CBird. Can anyone provide me with clarification to classes related to OOP? Thanks in advance. V/R Rob

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              rbwest86 wrote:

              "Is it reasonable to create a CHawk by deriving from CBird? How about a COstrich? Justify your answers. Derive an avian hierarchy that can cope with both of these birds."

              Both ostrich and hawk belong to the Aves class, so they are both birds. While an ostrich belongs to the Struthioniformes (flightless) order, it's a bird nonetheless.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              1 Reply Last reply
              0
              • R rbwest86

                Hello everyone, Just so its known this question is related to my programming class. Here is the question out of the book: "Is it reasonable to create a CHawk by deriving from CBird? How about a COstrich? Justify your answers. Derive an avian hierarchy that can cope with both of these birds."

                class CBird
                {
                protected:
                int wingSpan;
                int eggSize;
                int airSpeed;
                int altitude;
                public:
                virtual void fly() { altitude = 100; }
                };

                I thought that the class has to be declared first. Like #include CBird.h goes in the include list. If I am wrong, then the question from the book is YES, CHawk and COstrich can be inherited from CBird. Can anyone provide me with clarification to classes related to OOP? Thanks in advance. V/R Rob

                B Offline
                B Offline
                Bram van Kampen
                wrote on last edited by
                #7

                Hello, Well, I think the answer is in:

                rbwest86 wrote:

                Justify your answers

                That means in this case that nearly any answer will do, provided you can argue the point. I would say that they all can be derrived from CBird, they all potentially have eggSize, airSpeed and altitude. You distinguish their flying behaviour by overriding the Virtual Function 'fly' In the case of COstrich, I would say:

                #include 'Bird.h"
                class COstrich : public CBird{
                void COstrich::fly(){altitude=0;}

                Beyond that, what springs to mind is, that one fool can ask more questions...etc. Regards, :)

                Bram van Kampen

                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