Passing derived classes uninitialized variable to base class constructor !!

Discussion in 'C++' started by PSN, Sep 22, 2006.

  1. PSN

    PSN Guest

    Hi all ..
    can any one please explain the output of the following code ..

    class A {
    public:
    int a1;
    int a2;
    A(int a1, int a2)
    {
    cout << "i am in AAA" << endl;
    this->a1 = a1;
    this->a2 = a2;
    }
    };

    class B : public A {
    public:
    int b1;
    int b2;
    B(int b1, int b2, int a1, int a2) : A(b1, a2)
    {
    cout << "i am in BBB" << endl;
    this->b1 = b1;
    this->b2 = b2;
    }
    };

    int main()
    {
    B test(10, 20, 30, 40);
    cout << test.b1 << " " << test.b2 << " " << test.a1 << " " << test.a2
    << endl;
    }


    The result is:
    I am in AAA
    I am in BBB
    10, 20, 10, 40

    The constructor of A is called first .. therefore when i pass an
    uninitialized value, "b1", to the constructor of the base, i was
    expecting a crash or some junk value .. but i see that a1 is
    initialized to 10. But when i comment out the initialization statement
    of b1 (this->b1 = b1), i see a junk value for the variable a1 ... Can
    someone please explain me this ..

    thanks ...
    PSN, Sep 22, 2006
    #1
    1. Advertising

  2. PSN

    Bo Persson Guest

    PSN wrote:
    > Hi all ..
    > can any one please explain the output of the following code ..
    >
    > class A {
    > public:
    > int a1;
    > int a2;
    > A(int a1, int a2)
    > {
    > cout << "i am in AAA" << endl;
    > this->a1 = a1;
    > this->a2 = a2;
    > }
    > };
    >
    > class B : public A {
    > public:
    > int b1;
    > int b2;
    > B(int b1, int b2, int a1, int a2) : A(b1, a2)
    > {
    > cout << "i am in BBB" << endl;
    > this->b1 = b1;
    > this->b2 = b2;
    > }
    > };
    >
    > int main()
    > {
    > B test(10, 20, 30, 40);
    > cout << test.b1 << " " << test.b2 << " " << test.a1 << " " <<
    > test.a2
    > << endl;
    > }
    >
    >
    > The result is:
    > I am in AAA
    > I am in BBB
    > 10, 20, 10, 40
    >
    > The constructor of A is called first .. therefore when i pass an
    > uninitialized value, "b1", to the constructor of the base, i was
    > expecting a crash or some junk value .. but i see that a1 is
    > initialized to 10.


    You are not passing the member variable b1, but the constructor
    parameter b1 to A's constructor.

    > But when i comment out the initialization statement
    > of b1 (this->b1 = b1), i see a junk value for the variable a1 ...
    > Can
    > someone please explain me this ..


    I see a junk value for b1 (which is printed first!), not for a1.
    That's as expected.


    Bo Persson
    Bo Persson, Sep 22, 2006
    #2
    1. Advertising

  3. PSN

    PSN Guest

    I am sorry ...
    i understand it now ..

    the similar names made it confusing ..

    thanks a lot ..


    Bo Persson wrote:
    > PSN wrote:
    > > Hi all ..
    > > can any one please explain the output of the following code ..
    > >
    > > class A {
    > > public:
    > > int a1;
    > > int a2;
    > > A(int a1, int a2)
    > > {
    > > cout << "i am in AAA" << endl;
    > > this->a1 = a1;
    > > this->a2 = a2;
    > > }
    > > };
    > >
    > > class B : public A {
    > > public:
    > > int b1;
    > > int b2;
    > > B(int b1, int b2, int a1, int a2) : A(b1, a2)
    > > {
    > > cout << "i am in BBB" << endl;
    > > this->b1 = b1;
    > > this->b2 = b2;
    > > }
    > > };
    > >
    > > int main()
    > > {
    > > B test(10, 20, 30, 40);
    > > cout << test.b1 << " " << test.b2 << " " << test.a1 << " " <<
    > > test.a2
    > > << endl;
    > > }
    > >
    > >
    > > The result is:
    > > I am in AAA
    > > I am in BBB
    > > 10, 20, 10, 40
    > >
    > > The constructor of A is called first .. therefore when i pass an
    > > uninitialized value, "b1", to the constructor of the base, i was
    > > expecting a crash or some junk value .. but i see that a1 is
    > > initialized to 10.

    >
    > You are not passing the member variable b1, but the constructor
    > parameter b1 to A's constructor.
    >
    > > But when i comment out the initialization statement
    > > of b1 (this->b1 = b1), i see a junk value for the variable a1 ...
    > > Can
    > > someone please explain me this ..

    >
    > I see a junk value for b1 (which is printed first!), not for a1.
    > That's as expected.
    >
    >
    > Bo Persson
    PSN, Sep 22, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrstephengross
    Replies:
    5
    Views:
    585
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    559
    David Harmon
    Mar 5, 2007
  3. Replies:
    1
    Views:
    387
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    374
    Victor Bazarov
    May 23, 2007
  5. Rahul
    Replies:
    16
    Views:
    1,178
    terminator
    Nov 7, 2007
Loading...

Share This Page