Thread: How to copy a constructor from base to child classes

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    49

    How to copy a constructor from base to child classes

    While I was writing some code I noticed something:


    In this program


    Code:
    #include <iostream>
    
    
    class Character
    {
        public:
            Character(int health, int carryWeight, int stamina, int magicka, std::string name, std::string race)
                : mHealth(health),
                  mCarryWeight(carryWeight),
                  mStamina(stamina),
                  mMagicka(magicka),
                  mName(name),
                  mRace(race)
                  {}
    
    
            virtual void Speak()
            {
                std::cout << "Default Character\n";
            }
    
    
            void ShowCharacterInfo()
            {
                std::cout << "Health: " << mHealth << "\n";
                std::cout << "Carry Weight: " << mCarryWeight << "\n";
                std::cout << "Stamina: " << mStamina << "\n";
                std::cout << "Magicka: " << mMagicka << "\n";
                std::cout << "Name: " << mName << "\n";
                std::cout << "Race: " << mRace << "\n";
            }
    
    
        protected:
            int mHealth;
            int mCarryWeight;
            int mStamina;
            int mMagicka;
    
    
            std::string mName;
            std::string mRace;
    };
    
    
    class Nord : public Character
    {
        public:
            Nord(int health, int carryWeight, int stamina, int magicka, std::string name, std::string race)
                : Character(health, carryWeight, stamina, magicka, name, race){}
    
    
            void Speak()
            {
                std::cout << "I am a " << mRace << " my name is " << mName << "\n";
            }
    };
    
    
    int main()
    {
        Nord Ulfric(100, 100, 80, 190, "Ulfric", "Nord");
        Nord Harkon(250, 150, 50, 150, "Harkon", "Nord");
    
    
        Character& NordUlfric = Ulfric;
        Character& NordHarkon = Harkon;
    
    
        Ulfric.ShowCharacterInfo();
        std::cout << "\n";
        Harkon.ShowCharacterInfo();
    
    
        std::cout << "\n";
    
    
        Ulfric.Speak();
        Harkon.Speak();
    
    
    
    
        return 0;
    }

    This constructor in Nord:


    Code:
     Nord(int health, int carryWeight, int stamina, int magicka, std::string name, std::string race)
                : Character(health, carryWeight, stamina, magicka, name, race){}

    Is a pain to update everytime I add a variable to Character. I have to then edit the constructor in the other class. Is there a simpler way? Like is there a way that Nord can just inherit the constructor of Character so I dont have to keep updating it? I would have to do that for all other classes if I had more as well and that would start to be a real pain, there has to be an easier way, I want to be able to just update the constructor in Character and it updates for all that inherit from it.

  2. #2
    Registered User
    Join Date
    Feb 2015
    Posts
    49
    Ok so here is my updated code with a solution I found after hours of searching:


    Code:
    #include <iostream>
    
    
    class Character
    {
        public:
            Character(int health, int carryWeight, int stamina, int magicka, std::string name, std::string race)
                : mHealth(health),
                  mCarryWeight(carryWeight),
                  mStamina(stamina),
                  mMagicka(magicka),
                  mName(name),
                  mRace(race)
                  {}
    
    
            virtual void Speak()
            {
                std::cout << "I am a " << mRace << " my name is " << mName << "\n";
            }
    
    
            void ShowCharacterInfo()
            {
                std::cout << "Health: " << mHealth << "\n";
                std::cout << "Carry Weight: " << mCarryWeight << "\n";
                std::cout << "Stamina: " << mStamina << "\n";
                std::cout << "Magicka: " << mMagicka << "\n";
                std::cout << "Name: " << mName << "\n";
                std::cout << "Race: " << mRace << "\n";
            }
    
    
        protected:
            int mHealth;
            int mCarryWeight;
            int mStamina;
            int mMagicka;
    
    
            std::string mName;
            std::string mRace;
    };
    
    
    class Nord : public Character
    {
        public:
            //Nord(int health, int carryWeight, int stamina, int magicka, std::string name, std::string race)
                //: Character(health, carryWeight, stamina, magicka, name, race){}
            using Character::Character;
    };
    
    
    class Argonian : public Character
    {
        public:
            using Character::Character;
    };
    
    
    int main()
    {
        Nord Ulfric(100, 100, 80, 190, "Ulfric", "Nord");
        Nord Harkon(250, 150, 50, 150, "Harkon", "Nord");
    
    
        Character& NordUlfric = Ulfric;
        Character& NordHarkon = Harkon;
    
    
        Ulfric.ShowCharacterInfo();
        std::cout << "\n";
        Harkon.ShowCharacterInfo();
    
    
        std::cout << "\n";
    
    
        Ulfric.Speak();
        Harkon.Speak();
    
    
    
    
        return 0;
    }

    I just put
    Code:
    using Character::Character;
    in the public section and voila, it works, however I know that just because the compiler doesnt complain, it doesnt mean there isnt a problem, can anyone tell me if there is a drawback to this? I dont see any, I see a great benefit, it keeps me from having to re-write boilerplate code so thats a massive plus.


    Here is where I found this information:


    c++ - What is constructor inheritance? - Stack Overflow

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    955
    The fact that the Nord and Argonian classes are empty should be a clue that they are useless. You're even passing the string "Nord" or "Argonian" to Character!
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  4. #4
    Registered User
    Join Date
    Feb 2015
    Posts
    49
    Well, I havent finished writing them yet, I wanted to make sure the constructor copying wasnt doing anything weird before I moved on, they'll all have their own purpose soon enough, this program is more for just practice anyways, it's not a serious project.
    Last edited by CH1156; 08-25-2018 at 12:54 PM.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    955
    You will find in the end that it's better to just use the Character class.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  6. #6
    Registered User
    Join Date
    Feb 2015
    Posts
    49
    Sure, but I wanted to test out the constructor inheritance, which is mainly why I made the child classes, I agree they're useless otherwise.
    Last edited by CH1156; 08-25-2018 at 01:03 PM.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    955
    I see. There's nothing wrong with inheriting constructors with a using declaration. It's been available (standard-wise) since C++11:
    Using-declaration - cppreference.com
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  8. #8
    Registered User
    Join Date
    Feb 2015
    Posts
    49
    Awesome! thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base and Derived class copy constructor help.
    By byebyebyezzz in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2011, 07:10 AM
  2. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  3. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  4. Replies: 18
    Last Post: 11-21-2007, 10:38 AM
  5. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 11:57 AM

Tags for this Thread