1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

can a constructor inherited

Discussion in 'C++' started by Jaya, Sep 6, 2007.

  1. Jaya

    Jaya New Member

    Joined:
    Dec 13, 2006
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    can a constructor inherited
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    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.
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Can a bear shitted?
     
  4. Jaya

    Jaya New Member

    Joined:
    Dec 13, 2006
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Can a constructor of a class be inherited ? can u please explain in detail
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    If you don't read the response don't expect any thing more than what you see in replies as thread.
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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;
    }
    
     

Share This Page