473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Code Snippets

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

Jul 23 '05 #1
3 3565
"Ramesh Tharma" <ra********@yahoo.com> wrote in message
news:S1*******************@news20.bellglobal.com.. .
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


Jul 23 '05 #2
Why don't you compile it with your own favorite compiler and see what happens? :-)

"Ramesh Tharma" <ra********@yahoo.com> 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

Jul 23 '05 #3

"Uenal Mutlu" <52***************@t-online.de> wrote in message
news:d8*************@news.t-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.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Aurélien Géron | last post by:
Hi, Does anyone know where I can find a lot of Python code snippets? I searched the Python wiki and Internet but could not find more than five or ten code snippets at a time. I'm looking...
8
by: asmirnov1234567890 | last post by:
Hi I'd like to place this code to C++ code snippets site. Does anyone know about where is could be? Andrei Smirnov ======================================= // // example of how to strip...
2
by: - Steve - | last post by:
I'm working on my asp.net site and I'm wondering what the best way to reuse little snippets of code is? Right now I have a Class in it's own cs file that I call Snippets. Then when I want to...
2
by: Steve Franks | last post by:
I'm using VS.NET 2005 beta 2 and am excited about the idea of using code snippets. This is a nice time saver. For example I need to read all text from a file, and see that with a simple command...
5
by: Thomas | last post by:
hello, what is the best way to store code snippets? Is there a good plugin available or another good app? thomas
3
by: kai | last post by:
Hi, Base on MSDN help, in VS2005 IDE, select "Tools -> Code Snippets Manager", but I cannot find "Code Snippets Manager" under "Tools" menu. Also I noticed when I insert snippets in C#, it has...
10
by: Jak | last post by:
Hi, all, I am wondering if there are any addons for VB.NET 2005 to complete the codes automatically after I entered two or three characters, just like Delphi does. Thanks, Jack Zhong
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
1
by: StreamLogic | last post by:
All, I have posted this question in various groups, so I apologize for any overlap. I'd like to hear how other developers/architects keep track of old scripts, code snippets, relevant URLs,...
2
Frinavale
by: Frinavale | last post by:
I'm attempting to supplement the help for my code by providing other developers with code snippets that demonstrate how to use my classes/method. I've created a .snippet file and have placed it in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.