inheritance - constructor

This is a discussion on inheritance - constructor within the C++ Programming forums, part of the General Programming Boards category; Code: class Vector2_1 { protected: double a, b; public: Vector2_1() {a=0; b=0;} Vector2_1(double vec1, double vec2):a(vec1), b(vec2) {} void SetSpecificValue(double ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    49

    inheritance - constructor

    Code:
    class Vector2_1
    {
    protected:
    	double a, b;
    public:
    	Vector2_1() {a=0; b=0;}
    	Vector2_1(double vec1, double vec2):a(vec1), b(vec2) {}
    	void SetSpecificValue(double value, int pos);
    	double GetSpecificValue(int pos) const;
    	virtual void print() {cout<<endl<<"["<<a<<" "<<b<<"]T"<<endl;}
    };
    
    
    class Vector1_2:public Vector2_1
    {
    public:
    	Vector1_2 operator * (const Matrix2_2&) const;
    	void print() {cout<<endl<<"["<<a<<" "<<b<<"]<<endl;} //The problematic line
    };

    Two problems:
    1) For some reason it's like there is no inheritance between Vector1_2 from Vector2_1.
    I can't write
    Vector1_2 vec2(2,3);
    It says there is no such overloaded function...

    2) Adding the print() at the daughter class results in a large collection of errors, I guess from something related to the first point.

    Thank you.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    All classes need their own constructor. The compiler will automatically call the default constructor of the base-class, but the derived class needs to have a constructor.

    Your print function in the derived class has "] without an end quote, which can indeed lead to all sorts of errors.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Constructors are not inherited.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Thank you very much
    Last edited by misterowakka; 01-17-2008 at 12:46 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    I think it makes more sense to think in that the base class's constructor is only used to construct the base class itself, and not the derived class. When you type Vector1_2 vec2(2, 3), it tries to find a constructor for Vector1_2 that takes two ints and will not look in the base class.
    All functions are inherited (though I dunno about constructors), even if private, just... they can't be accessed if they're private.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by laserlight View Post
    Constructors are not inherited.
    Depends how you look at it... The constructor is still there, and in fact must be there, so the derived class's constructor can call it first before constructing itself. But the user of the class has no way to access it.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by brewbuck View Post
    Depends how you look at it... The constructor is still there, and in fact must be there, so the derived class's constructor can call it first before constructing itself. But the user of the class has no way to access it.
    So, what's the difference?

    Code:
    Vector1_2(double vec1, double vec2) {Vector2_1(vec1,vec2);}
    Vector1_2(double vec1, double vec2) {a=vec1; b=vec2;}
    The first one initializes a and b using the default constructor of the base class even though I called the overloaded one...
    The second form acts well...

    Why?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    The derived class can access the base class's constructor, since it's just a function of the base class, but the code outside the class can't construct an object through the base class's constructor. It can only call a derived constructor when creating the object.

    You could, of course, explicitly call the base class's constructor after you've created the object, but what's the point?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    So, what's the difference?
    One can argue that constructors are inherited since the derived class constructors can use them. One could argue that they are not inherited since, being special member functions, constructors are invoked via special syntax, and with this kind of syntax it is impossible, regardless of public/protected/private access, for the user of the class (or indeed member functions of the class other than the constructors) to access base class constructors directly.

    The first one initializes a and b using the default constructor of the base class even though I called the overloaded one...
    You should be using an initialisation list:
    Code:
    Vector1_2(double vec1, double vec2) : Vector2_1(vec1,vec2) {}
    // or
    Vector1_2(double vec1, double vec2) : a(vec1), b(vec2) {}
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    The first one initializes a temp object which immediately gets thrown away. It does nothing to initialize the base class at all, it does not touch the base Vector2_1 object at all. What you probably want is to use the initializer list for the 1st Vector1_2 constructor to construct the base Vector2_1 object using those parameters:
    Code:
    Vector1_2(double vec1, double vec2) : Vector2_1(vec1,vec2) {}
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Code:
    class Vector2_1
    {
    protected:
    	double a, b;
    public:
    	Vector2_1() {a=0; b=0;}
    	Vector2_1(double vec1, double vec2):a(vec1), b(vec2) {}
    	void SetSpecificValue(double value, int pos);
    	double GetSpecificValue(int pos) const;
    	virtual void print() {cout<<endl<<"["<<a<<" "<<b<<"]T"<<endl;}
    	Vector1_2 transpose() {return Vector1_2(a,b);}  //the problem
    	Vector2_1 operator / (const double& C);
    };
    
    
    
    class Vector1_2:public Vector2_1
    {
    public:
    	Vector1_2() {Vector2_1();}
    	//Vector1_2(double vec1, double vec2) {Vector2_1(vec1,vec2);}
    	Vector1_2(double vec1, double vec2) {a=vec1; b=vec2;}
    	Vector1_2 operator * (const Matrix2_2&) const;
    	void print() {cout<<endl<<"["<<a<<" "<<b<<"]"<<endl;}
    	Vector2_1 transpose() {return Vector2_1(a,b);}
    };


    error C2501: 'Vector2_1::Vector1_2' : missing storage-class or type specifiers
    error C2146: syntax error : missing ';' before identifier 'transpose'
    error C2146: syntax error : missing ';' before identifier 'transpose'
    error C2501: 'Vector2_1::Vector1_2' : missing storage-class or type specifiers

    I have a function in the base class that generates an instance of the derived class. I assume that the problem is that the base class doesn't know the derived class because it is beneath it, therefore, I tried to declare the classes at the head of the header

    Code:
    class Vector2_1;
    class Matrix2_2;
    class Vector1_2:class Vector2_1;
    This results in a massive amount of errors.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    This is not possible.
    Vector1_2 needs to be defined before Vector2_1 for this to work because you're returning a new object of type Vector1_2, thus it needs to call the constructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by Elysia View Post
    This is not possible.
    Vector1_2 needs to be defined before Vector2_1 for this to work because you're returning a new object of type Vector1_2, thus it needs to call the constructor.
    It's not working and I guess because the derived class, when placed on top, don't know the base class, and declaring the base class in the beginning doesn't help too.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Indeed. The solution to such a problem is to rethink your design or return pointers so it doesn't need to call the constructor in the header.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm sorry for not being very familiar with these things but...

    What is the difference between Vector1_2 and Vector2_1 and why is one inherited from the other and not, perhaps, the other way round.? (Especially seeing that transpose should return the other for both.)

    As I see the only difference is that one prints "T" in addition to the common things, and methods are rather randomly distributed between the two. Couldn't you put a bool in there to show that it is or isn't transposed? (Does it even matter if it is transposed? Wouldn't it be up to the user to decide what the vector represents?)
    Last edited by anon; 01-17-2008 at 02:08 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 09:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 02:59 AM
  3. How do I override constructor in inheritance?
    By Loduwijk in forum C++ Programming
    Replies: 13
    Last Post: 03-24-2006, 09:36 AM
  4. Constructor inheritance
    By Hunter2 in forum C++ Programming
    Replies: 9
    Last Post: 07-14-2004, 11:13 AM
  5. Constructor inheritance
    By Jasel in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2003, 10:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21