Classes and Inheritance, question inside.
-
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
-
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
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
-
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
-
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?
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.
-
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?
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
-
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
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
-
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
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