Thread: Do constructors get inherited?

  1. #1
    Shadow12345
    Guest

    Do constructors get inherited?

    I mean do they? It doesn't seem that they do, that's just silly

    Code:
    class Human : public Parent {
    public:
    	Human(string name);
    
    
    private:
    
    
    };
    
    Human::Human(string name) {
    	Name = name;
    }
    Human is a derived class of Parent. parent is technically a human as well.

    I get this error:
    [error]
    Compiling...
    human.cpp
    C:\human.cpp(162) : error C2512: 'Parent' : no appropriate default constructor available
    Error executing cl.exe.

    human.exe - 1 error(s), 0 warning(s)
    [/error]

    that is really silly, I mean more silly than silly can possibly be!
    Last edited by Shadow12345; 08-21-2002 at 09:47 AM.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yes they do. the base class constructor is called first, then the derived class.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    So before you construct Human, you must construct Parent...
    (shouldn't Parent be derived from Human? )

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Originally posted by BMJ
    So before you construct Human, you must construct Parent...
    (shouldn't Parent be derived from Human? )
    No, he got it right. lol

  5. #5
    Shadow12345
    Guest
    parent in this case should absolutely not be derived from human, because each human must be created from two parents

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Make the partent constructor virtual if I remember correctly.

  7. #7
    Shadow12345
    Guest
    This is my parent constructor so far (emphasize SO FAR)
    Code:
    Parent::Parent(string name) {
    
    	Name = name;
    	
    	SkinColor = rand()%5;
    
    	HairColor = rand()%5;
    
    	EyeColor = rand()%5;
    
    	Height = (rand()%36) + 36; //mexico sucks
    
    	Weight = (rand()%200) + 70;
    
    	Intelligence = rand()%5;
    
    
    	
    };
    Here is my human constructor so far
    Code:
    class Human : public Parent {
    public:
    	Human(string name);
    
    
    private:
    
    
    };
    Can you see in those two constructors why I might be getting that error? I'll post the error again
    Compiling...
    human.cpp
    C:\human.cpp(162) : error C2512: 'Parent' : no appropriate default constructor available
    Error executing cl.exe.

    human.exe - 1 error(s), 0 warning(s)

    The error points to the Human::Human constructor
    should I change it to Parent::Human or something...frig

  8. #8
    Shadow12345
    Guest
    You can't make constructors virtual!!!!!!!!!!!!!!

  9. #9
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    yep

  10. #10
    Unregistered
    Guest
    You Should pass arguements to the Parent Constructor from the Human Class Constructor. So, You should define the Human Constructor like this.

    Human::Human(string ParentString, string HumanString)
    {
    Parent(ParentSring);
    name = HumanString;
    }

  11. #11
    Shadow12345
    Guest
    Ok well I don't even need to define a Human Constructor, because the Parent constructor does all that I need. However, when I don't do that I get the following errors:
    [error]
    --------------------Configuration: human - Win32 Debug--------------------
    Compiling...
    human.cpp
    C:\human.cpp(170) : error C2664: 'Human::Human' : cannot convert parameter 1 from 'char [8]' to 'const class Human &'
    Reason: cannot convert from 'char [8]' to 'const class Human'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\human.cpp(172) : error C2664: 'Human::Human' : cannot convert parameter 1 from 'char [8]' to 'const class Human &'
    Reason: cannot convert from 'char [8]' to 'const class Human'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\human.cpp(174) : error C2664: 'Human::Human' : cannot convert parameter 1 from 'char [4]' to 'const class Human &'
    Reason: cannot convert from 'char [4]' to 'const class Human'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\human.cpp(176) : error C2664: 'Human::Human' : cannot convert parameter 1 from 'char [6]' to 'const class Human &'
    Reason: cannot convert from 'char [6]' to 'const class Human'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\human.cpp(178) : error C2664: 'Human::Human' : cannot convert parameter 1 from 'char [5]' to 'const class Human &'
    Reason: cannot convert from 'char [5]' to 'const class Human'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\human.cpp(180) : error C2664: 'Human::Human' : cannot convert parameter 1 from 'char [7]' to 'const class Human &'
    Reason: cannot convert from 'char [7]' to 'const class Human'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Error executing cl.exe.

    human.exe - 6 error(s), 0 warning(s)
    [/error]

    Those errors are called everytime I make an instance of a human and try to assign it a name
    i.e

    Human Rachel("Rachel");

  12. #12
    Nick
    Guest
    If your base class has a non default constructor then
    the devived class has to call it like this

    Code:
    class A  {
    public:
           A(int n);
    };
    
    class B : public A {
    public:
            B() : A(4) { }
    };

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Troll_King
    Make the partent constructor virtual if I remember correctly.
    nope. you're thinking destructors
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    Unregistered
    Guest
    So, You don't want to do anything with the Base Class Constructor(Parent) , then include default constructor in the Base class.

    class Parent

    public:

  15. #15
    Unregistered
    Guest
    So, You don't want to do anything with the Base Class Constructor(Parent) , then include default constructor in the Base class.

    class Parent
    {

    public:
    Parent()
    {
    }
    Parent(string Name)
    {
    name = Name;
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Inherited Classes Needed
    By elliott in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2005, 09:05 AM
  2. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  3. Inherited constructors with parameters
    By dave74 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2003, 11:27 AM
  4. Replies: 4
    Last Post: 12-29-2002, 12:29 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM