Code Snippets

R

Ramesh Tharma

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
 
P

Phil Staite

Ramesh Tharma said:
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
 
P

Phil Staite

Uenal Mutlu said:
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.
 
U

Uenal Mutlu

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



Ramesh Tharma said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top