Code Snippets

Discussion in 'C++' started by Ramesh Tharma, Jun 8, 2005.

  1. Hi Guys,

    I was asked the following question in an interview, is anyone knows the
    anwers for these questions?

    What output does the following code generate? Why?
    What output does it generate if you make A::Foo() a pure virtual function?

    #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;
    }

    --------------------------------------------------------------------------------

    What output does this program generate as shown? Why?

    #include <iostream>

    using namespace std;

    class A {
    public:
    A() {
    cout << "A::A()" << endl;
    }
    ~A() {
    cout << "A::~A()" << endl; throw
    "A::exception";
    }
    };

    class B {
    public:
    B() {
    cout << "B::B()" << endl; throw
    "B::exception";
    }
    ~B() {
    cout << "B::~B()";
    }
    };

    int main(int, char**) {
    try {
    cout << "Entering try...catch block" << endl;

    A objectA;
    B objectB;

    cout << "Exiting try...catch block" << endl;
    } catch (char* ex) {
    cout << ex << endl;
    }

    return 0;
    }Thanks,Ramesh
     
    Ramesh Tharma, Jun 8, 2005
    #1
    1. Advertising

  2. Ramesh Tharma

    Phil Staite Guest

    "Ramesh Tharma" <> wrote in message
    news:S1tpe.7118$...
    > Hi Guys,
    >
    > I was asked the following question in an interview, is anyone knows the
    > anwers for these questions?


    Not to sound too harsh but... If you don't know the answers to these kinds
    of simple questions, I wouldn't be interviewing for a C++ programming
    position.

    > What output does the following code generate? Why?


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

    Because during construction of the B in main() the A (base class) part is
    constructed first. At the time the A constructor runs, all it is is an A.
    The B part hasn't been made yet, so the current virtual function table still
    points to the A implementation. Once B's constructor is executing, the vtbl
    points to the B functions.

    > What output does it generate if you make A::Foo() a pure virtual function?


    Actually, that depends. ;-) If you declare it a pure virtual, but still
    provide an implementation for it, no change. If you declare it pure virtual
    and do not provide an implementation for it - pure virtual function call at
    runtime.

    > --------------------------------------------------------------------------

    ------
    >
    > What output does this program generate as shown? Why?


    I *believe* something like:

    Entering try...catch block
    {unhandled exception message}

    Because objects are destructed in reverse order of their construction,
    objectB is destructed first. The body of ~B() executes first, destroying
    the B part of the object, and throws. (did you know that exceptions in
    destructors are an extremely bad thing?) Execution boils out the catch
    block but... Another guarantee of C++ is that all local objects that finish
    construction are destructed before exiting the scope... So on the way out
    of the try block, it will destruct objectA - which also throws. This double
    exception is a very bad thing. (did I mention exceptions in destructors are
    bad?)


    Note, these answers are off the top of my head, I didn't try compiling...
    YMMV
     
    Phil Staite, Jun 8, 2005
    #2
    1. Advertising

  3. Ramesh Tharma

    Phil Staite Guest

    "Uenal Mutlu" <> wrote in message
    news:d85s7s$jms$00$-online.com...
    > Why don't you compile it with your own favorite compiler and see what

    happens? :)

    I did that after posting my reply and realized a couple of things...

    a) Shouldn't reply after a long day followed by a couple of margaritas. But
    a sudden shock and a little humility go a long way towards sobering you up.
    b) I disagree with Borland C++ Builder - it still gives a pure virtual
    function call when A::Foo() is declared pure virtual but has a body defined.
    But I'm too tired to go search in the standard or reboot into Fedora Core
    and try it with a more up to date gcc. Now I'm wondering if my
    interpretation of this situation is in error due to prior experience where a
    compiler allowed it? Or am I thinking of explicitly calling up to A::Foo()
    via scope override? Why else would anyone provide a body for a pure
    virtual? (or be allowed to?)
    c) I should've read the second program closer or reformatted it so I'd see
    that the throw in B is in the constructor, not the destructor. And I forgot
    to include the cout output of the constructors... Sheesh. The gist was
    correct though, just not the exact sequence. You end up with a double
    exception and bailing out to the OS.
     
    Phil Staite, Jun 8, 2005
    #3
  4. Ramesh Tharma

    Uenal Mutlu Guest

    Why don't you compile it with your own favorite compiler and see what happens? :)



    "Ramesh Tharma" <> wrote
    >
    > I was asked the following question in an interview, is anyone knows the
    > anwers for these questions?
    >
    > What output does the following code generate? Why?
    > What output does it generate if you make A::Foo() a pure virtual function?
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class A {
    > public:
    > A() {
    > this->Foo();
    > }


    // snip
     
    Uenal Mutlu, Jun 8, 2005
    #4
    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. - Steve -

    Reusing Code Snippets

    - Steve -, Sep 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    442
    Greg Burns
    Oct 1, 2004
  2. Pat

    Where are my Code Snippets?

    Pat, Apr 20, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    406
    Simon Harris
    Apr 23, 2005
  3. Steve Franks
    Replies:
    2
    Views:
    374
    Juan T. Llibre
    Sep 11, 2005
  4. Edwin Knoppert
    Replies:
    0
    Views:
    380
    Edwin Knoppert
    Nov 30, 2005
  5. Replies:
    14
    Views:
    753
    Flash Gordon
    Nov 20, 2007
Loading...

Share This Page