Help with Inheritance
-
Hi there Below is the code i have written, is there something wrong with my derived class? i am getting a compiler error :No default Constructor available for the Animal class. Or is it the base class ,
class Animal { public: Animal(const string &name, const char &sex, const string &Colour, int age); ~Animal(); //get amount of food for specific animal int feed_animal(const int &); void display_Info() const; private: string aName; char aSex; string aColour; int aAge; }; class Crocodile : public Animal { public: Crocodile(int jaw_size, const string &move_ment); ~Crocodile(); private: int jaw; string move; }; Animal::Animal(const string &name,const char &sex, const string &Colour, int Age) :aName(name), aSex(sex), aColour(Colour), aAge(age) { //Empty Constructor } Animal::~Animal() { } int Animal::feed_animal(const int &food) { return food; } void Animal::display_Info() const { cout << "Name : " << aName << endl; cout << "Sex : " << aSex << endl; cout << "Colour : " << aColour << endl; cout << "Age : " << aAge << endl; } int main(int argc, char *argv[]) { Animal Crocko("American Aligator", 'M', "Dark Grey", 2); Crocko.feed_animal(25); //In kilograms or grams Crocko.display_animal(); return 0; }
thanks in advance Bhangie Education begins a gentleman, conversation completes him ;) -
Hi there Below is the code i have written, is there something wrong with my derived class? i am getting a compiler error :No default Constructor available for the Animal class. Or is it the base class ,
class Animal { public: Animal(const string &name, const char &sex, const string &Colour, int age); ~Animal(); //get amount of food for specific animal int feed_animal(const int &); void display_Info() const; private: string aName; char aSex; string aColour; int aAge; }; class Crocodile : public Animal { public: Crocodile(int jaw_size, const string &move_ment); ~Crocodile(); private: int jaw; string move; }; Animal::Animal(const string &name,const char &sex, const string &Colour, int Age) :aName(name), aSex(sex), aColour(Colour), aAge(age) { //Empty Constructor } Animal::~Animal() { } int Animal::feed_animal(const int &food) { return food; } void Animal::display_Info() const { cout << "Name : " << aName << endl; cout << "Sex : " << aSex << endl; cout << "Colour : " << aColour << endl; cout << "Age : " << aAge << endl; } int main(int argc, char *argv[]) { Animal Crocko("American Aligator", 'M', "Dark Grey", 2); Crocko.feed_animal(25); //In kilograms or grams Crocko.display_animal(); return 0; }
thanks in advance Bhangie Education begins a gentleman, conversation completes him ;)In C++, a derived class's constructor calls the base class constructor first before proceeding. In your
Crocodile
class, you have a constructor that takes two arguments. You haven't called the base class constructor explicitly, so the compiler puts a calls to the base class' default constructor. Because there is no default constructor, the code fails to compile. You can make it by compile by adding a default constructor toAnimal
, likeclass Animal
{
public:
Animal() {}
// Your code here
};Or you can propagate the call from the derived class to the base class with something like
class Crocodile : public Animal
{
public:
Crocodile(const string &name, const char &sex, const string &Colour, int age,int jaw_size, const string &move_ment) : Animal(name, sex, Colour, age)
{
}
};By the way, I don't think deriving Crocodile from Animal is a good idea. A crocodile is an instance of animal. It's like inheriting Toyota Corolla and Mercedes Benz from Car. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Hi there Below is the code i have written, is there something wrong with my derived class? i am getting a compiler error :No default Constructor available for the Animal class. Or is it the base class ,
class Animal { public: Animal(const string &name, const char &sex, const string &Colour, int age); ~Animal(); //get amount of food for specific animal int feed_animal(const int &); void display_Info() const; private: string aName; char aSex; string aColour; int aAge; }; class Crocodile : public Animal { public: Crocodile(int jaw_size, const string &move_ment); ~Crocodile(); private: int jaw; string move; }; Animal::Animal(const string &name,const char &sex, const string &Colour, int Age) :aName(name), aSex(sex), aColour(Colour), aAge(age) { //Empty Constructor } Animal::~Animal() { } int Animal::feed_animal(const int &food) { return food; } void Animal::display_Info() const { cout << "Name : " << aName << endl; cout << "Sex : " << aSex << endl; cout << "Colour : " << aColour << endl; cout << "Age : " << aAge << endl; } int main(int argc, char *argv[]) { Animal Crocko("American Aligator", 'M', "Dark Grey", 2); Crocko.feed_animal(25); //In kilograms or grams Crocko.display_animal(); return 0; }
thanks in advance Bhangie Education begins a gentleman, conversation completes him ;)bhangie wrote: Below is the code i have written, is there something wrong Provide A Default Constructor to your class, like this
class Animal
{
public:
Animal();
Animal(const string &name, const char &sex, const string &Colour, int age);
~Animal();
---- SNip----------
}
Animal::Animal()
{
}"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta
-
In C++, a derived class's constructor calls the base class constructor first before proceeding. In your
Crocodile
class, you have a constructor that takes two arguments. You haven't called the base class constructor explicitly, so the compiler puts a calls to the base class' default constructor. Because there is no default constructor, the code fails to compile. You can make it by compile by adding a default constructor toAnimal
, likeclass Animal
{
public:
Animal() {}
// Your code here
};Or you can propagate the call from the derived class to the base class with something like
class Crocodile : public Animal
{
public:
Crocodile(const string &name, const char &sex, const string &Colour, int age,int jaw_size, const string &move_ment) : Animal(name, sex, Colour, age)
{
}
};By the way, I don't think deriving Crocodile from Animal is a good idea. A crocodile is an instance of animal. It's like inheriting Toyota Corolla and Mercedes Benz from Car. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
S. Senthil Kumar wrote: It's like inheriting Toyota Corolla and Mercedes Benz from Car. What about Roll Royce! :-D
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta
-
Hi there Below is the code i have written, is there something wrong with my derived class? i am getting a compiler error :No default Constructor available for the Animal class. Or is it the base class ,
class Animal { public: Animal(const string &name, const char &sex, const string &Colour, int age); ~Animal(); //get amount of food for specific animal int feed_animal(const int &); void display_Info() const; private: string aName; char aSex; string aColour; int aAge; }; class Crocodile : public Animal { public: Crocodile(int jaw_size, const string &move_ment); ~Crocodile(); private: int jaw; string move; }; Animal::Animal(const string &name,const char &sex, const string &Colour, int Age) :aName(name), aSex(sex), aColour(Colour), aAge(age) { //Empty Constructor } Animal::~Animal() { } int Animal::feed_animal(const int &food) { return food; } void Animal::display_Info() const { cout << "Name : " << aName << endl; cout << "Sex : " << aSex << endl; cout << "Colour : " << aColour << endl; cout << "Age : " << aAge << endl; } int main(int argc, char *argv[]) { Animal Crocko("American Aligator", 'M', "Dark Grey", 2); Crocko.feed_animal(25); //In kilograms or grams Crocko.display_animal(); return 0; }
thanks in advance Bhangie Education begins a gentleman, conversation completes him ;) -
In C++, a derived class's constructor calls the base class constructor first before proceeding. In your
Crocodile
class, you have a constructor that takes two arguments. You haven't called the base class constructor explicitly, so the compiler puts a calls to the base class' default constructor. Because there is no default constructor, the code fails to compile. You can make it by compile by adding a default constructor toAnimal
, likeclass Animal
{
public:
Animal() {}
// Your code here
};Or you can propagate the call from the derived class to the base class with something like
class Crocodile : public Animal
{
public:
Crocodile(const string &name, const char &sex, const string &Colour, int age,int jaw_size, const string &move_ment) : Animal(name, sex, Colour, age)
{
}
};By the way, I don't think deriving Crocodile from Animal is a good idea. A crocodile is an instance of animal. It's like inheriting Toyota Corolla and Mercedes Benz from Car. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
S. Senthil Kumar wrote: It's like inheriting Toyota Corolla and Mercedes Benz from Car. What is wrong with that? They are both cars.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
S. Senthil Kumar wrote: It's like inheriting Toyota Corolla and Mercedes Benz from Car. What is wrong with that? They are both cars.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
How do you then distinguish between instances and derived classes? My idea is that a class is a "template" and instances of the class are the "live" entities. Going by that logic, a specific car (say a Benz) is an instance of type car. I'd consider AutomaticTransmissionCar and ManualTransmissionCar to be subclasses of Car, for example. What do you say? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
How do you then distinguish between instances and derived classes? My idea is that a class is a "template" and instances of the class are the "live" entities. Going by that logic, a specific car (say a Benz) is an instance of type car. I'd consider AutomaticTransmissionCar and ManualTransmissionCar to be subclasses of Car, for example. What do you say? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Consider the following hierarchy:
-----------Vehicle ----------- / | \\ Car Boat Aircraft
/ \ / \ / \
Truck Van Sailboat Yacht Helicopter BlimpNote that
Yacht
is derived fromBoat
, which is derived fromVehicle
, but there are no instances of any of the classes until one is defined. You define an instance like:Helicopter whirlybird;
Here
whirlybird
is an instance of theHelicopter
class.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Consider the following hierarchy:
-----------Vehicle ----------- / | \\ Car Boat Aircraft
/ \ / \ / \
Truck Van Sailboat Yacht Helicopter BlimpNote that
Yacht
is derived fromBoat
, which is derived fromVehicle
, but there are no instances of any of the classes until one is defined. You define an instance like:Helicopter whirlybird;
Here
whirlybird
is an instance of theHelicopter
class.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
Yeah, but Yacht is not related to Boat in the same way as Benz is to Car. You can have several models of Yachts which have exactly the same properties but different values for them. But a Yacht is a specific kind of Boat with other properties. Likewise, a Benz and a Corolla have the properties of a car but have different values. DavidCrow wrote: Here whirlybird is an instance of the Helicopter class. I thought that's what I said too (I'm assuming whirlybird is a helicopter model). Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Yeah, but Yacht is not related to Boat in the same way as Benz is to Car. You can have several models of Yachts which have exactly the same properties but different values for them. But a Yacht is a specific kind of Boat with other properties. Likewise, a Benz and a Corolla have the properties of a car but have different values. DavidCrow wrote: Here whirlybird is an instance of the Helicopter class. I thought that's what I said too (I'm assuming whirlybird is a helicopter model). Regards Senthil _____________________________ My Blog | My Articles | WinMacro
"Derived from" and "instance of" are mutually exclusive terms. By definition, an instance of class is the actual occurrence of that class. In your example, Benz is not derived from anything. It is an instance of the Car class. S. Senthil Kumar wrote: I thought that's what I said too You said something about an instance being a live entity, whatever that is.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
"Derived from" and "instance of" are mutually exclusive terms. By definition, an instance of class is the actual occurrence of that class. In your example, Benz is not derived from anything. It is an instance of the Car class. S. Senthil Kumar wrote: I thought that's what I said too You said something about an instance being a live entity, whatever that is.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
You said "Sure it is. A Yacht is a Boat, and a Benz is a Car, and they are all Vehicles. However, until one of those objects actually exist, there are no instances". Going by that logic, why not derive whirlybird from Helicopter? whirlybird is a helicopter, after all. Then DavidCrow, SenthilKumar, instead of being instances of Humans, would be subclasses of Humans... DavidCrow wrote: By definition, an instance of class is the actual occurrence of that class. I agree and that's why I don't think Benz should be derived from Car. Regards Senthil _____________________________ My Blog | My Articles | WinMacro