Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Code Snippets

Reply
Thread Tools

Code Snippets

 
 
Ramesh Tharma
Guest
Posts: n/a
 
      06-08-2005
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



 
Reply With Quote
 
 
 
 
Phil Staite
Guest
Posts: n/a
 
      06-08-2005
"Ramesh Tharma" <(E-Mail Removed)> wrote in message
news:S1tpe.7118$(E-Mail Removed).. .
> 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




 
Reply With Quote
 
 
 
 
Phil Staite
Guest
Posts: n/a
 
      06-08-2005

"Uenal Mutlu" <(E-Mail Removed)> wrote in message
news:d85s7s$jms$00$(E-Mail Removed)-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.



 
Reply With Quote
 
Uenal Mutlu
Guest
Posts: n/a
 
      06-08-2005
Why don't you compile it with your own favorite compiler and see what happens?



"Ramesh Tharma" <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
`Comparability' of code snippets (from book `Code complete') lovecreatesbea...@gmail.com C Programming 14 11-20-2007 09:02 PM
Code snippets, tool to convert basic to .snippet xml? Edwin Knoppert ASP .Net 0 11-30-2005 03:45 PM
No code snippets built in yet for C# ASP.NET 2.0 beta? Steve Franks ASP .Net 2 09-11-2005 10:36 AM
Where are my Code Snippets? Pat ASP .Net 4 04-23-2005 01:29 PM
Reusing Code Snippets - Steve - ASP .Net 2 10-01-2004 11:29 AM



Advertisments