output of the program???

Discussion in 'C++' started by ujjwal bhoota, Oct 29, 2003.

  1. Hi,
    Can any one explain me the behaviour of following program.




    #include <iostream>

    using namespace std;

    class A {
    public:
    A() {
    this->Foo();
    }
    virtual void Foo() {
    cout << "A::Foo()" << endl;
    }
    };



    class B : public A {
    public:
    B() {
    this->Foo();
    }
    virtual void Foo() {
    cout << "B::Foo()" << endl;
    }
    };






    int main(int, char**)
    {
    B objectB;

    return 0;
    }



    The output is

    A::Foo()
    B::Foo()



    should not it be

    B::Foo()
    B::Foo()
    as B::Foo() has been declared to be virtual


    thanks and regards
    UJJWAL
    ujjwal bhoota, Oct 29, 2003
    #1
    1. Advertising

  2. ujjwal bhoota

    lilburne Guest

    ujjwal bhoota wrote:

    >
    > The output is
    >
    > A::Foo()
    > B::Foo()
    >
    >
    >
    > should not it be
    >
    > B::Foo()
    > B::Foo()
    > as B::Foo() has been declared to be virtual
    >


    B has not been constructed until after the A constructor has
    finished, therefore there is no B for B::foo() to be called
    whilst in A's constructor.
    lilburne, Oct 29, 2003
    #2
    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. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    591
    John Bentley
    Sep 10, 2003
  2. chuck amadi
    Replies:
    1
    Views:
    490
    Larry Bates
    Jun 23, 2004
  3. Puneet
    Replies:
    16
    Views:
    1,126
    Daniel Vallstrom
    Mar 20, 2005
  4. Paul
    Replies:
    1
    Views:
    430
  5. jmr
    Replies:
    2
    Views:
    335
Loading...

Share This Page