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. Help with Inheritance

Help with Inheritance

Scheduled Pinned Locked Moved C / C++ / MFC
helpoopquestionlearning
11 Posts 4 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.
  • B Offline
    B Offline
    bhangie
    wrote on last edited by
    #1

    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 ;)

    S T B 3 Replies Last reply
    0
    • B bhangie

      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 ;)

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      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 to Animal, like

      class 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

      T D 2 Replies Last reply
      0
      • B bhangie

        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 ;)

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • S S Senthil Kumar

          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 to Animal, like

          class 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

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • B bhangie

            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 ;)

            B Offline
            B Offline
            bhangie
            wrote on last edited by
            #5

            Thank's for the help oaks.:) Bhangie Education begins a gentleman, conversation completes him ;)

            1 Reply Last reply
            0
            • S S Senthil Kumar

              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 to Animal, like

              class 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

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

              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 1 Reply Last reply
              0
              • D David Crow

                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 Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                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

                D 1 Reply Last reply
                0
                • S S Senthil Kumar

                  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

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

                  Consider the following hierarchy:

                     -----------Vehicle -----------
                    /              |               \\
                  Car            Boat            Aircraft
                  

                  / \ / \ / \
                  Truck Van Sailboat Yacht Helicopter Blimp

                  Note that Yacht is derived from Boat, which is derived from Vehicle, 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 the Helicopter class.


                  "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                  S 1 Reply Last reply
                  0
                  • D David Crow

                    Consider the following hierarchy:

                       -----------Vehicle -----------
                      /              |               \\
                    Car            Boat            Aircraft
                    

                    / \ / \ / \
                    Truck Van Sailboat Yacht Helicopter Blimp

                    Note that Yacht is derived from Boat, which is derived from Vehicle, 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 the Helicopter class.


                    "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #9

                    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

                    D 1 Reply Last reply
                    0
                    • S S Senthil Kumar

                      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

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

                      "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

                      S 1 Reply Last reply
                      0
                      • D David Crow

                        "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

                        S Offline
                        S Offline
                        S Senthil Kumar
                        wrote on last edited by
                        #11

                        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

                        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