can a constructor inherited

Jaya's Avatar, Join Date: Dec 2006
Light Poster
can a constructor inherited
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Jaya
can a constructor inherited
Can you frame the question correctly? I don't understand what you meant by constructor being inherited? Do you mean to say syntactically or semantically.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Can a bear shitted?
Jaya's Avatar, Join Date: Dec 2006
Light Poster
Can a constructor of a class be inherited ? can u please explain in detail
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Jaya
Can a constructor of a class be inherited ? can u please explain in detail
If you don't read the response don't expect any thing more than what you see in replies as thread.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Consider testing your hypotheses or resolving your questions by writing a small piece of code and checking the outcome. It's a good learning method.
Code:
#include<iostream>
using std::cout;
using std::endl;

class parent
{
public:
    int x;
    parent ()
    {
        cout << "\tThis is the parent constructor" << endl;
        x = 0;
    }
};

class child: public parent
{
public:
    int x;
};

class sibling: public parent
{
public:
    int x;
    sibling ()
    {
        cout << "\tThis is the sibling constructor" << endl;
        x = 2;
    }
};
int main()
{
    cout << "Instantiating the child" << endl;
    child theKid;
    cout << "Instantiating the sibling" << endl;;
    sibling theBro;
    theKid.x = 4;
    cout << "Child x = " << theKid.x << endl;
    cout << "Sibling x = " << theBro.x << endl;
    return 0;
}
Quote:
Originally Posted by The output
Code:
Instantiating the child
        This is the parent constructor
Instantiating the sibling
        This is the parent constructor
        This is the sibling constructor
Child x = 4
Sibling x = 2