Google Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 0 vote

If it's a pure virtual function, you can't create an instance of that object.

- loky November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do not understand the second question. You mean the virtual functions of the current object? Is not it have been constructed yet?

- Anonymous November 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer he is looking for is that you shouldn't call virtual functions from a constructor because the vtable has not yet been setup at instantiation. So it will call one of the base classes function I believe.

- Anonymous November 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 votes

No, it will call the local version instead.

- Anonymous November 08, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

THats not true, it won't call the overridden version, look up vtables.

- Anonymous November 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey guys,

It gives call to the function from the same class for which the constructor is working. If it is a pure virtual function then in that case an error or exception is thrown.

- Aditya November 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it's a pure virtual function, you can't create an instance of that object.

- loky November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

guys..can u give me a good reference where i could read about vtables. i've never heard of them.

- Sharath January 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

http://en.wikipedia.org/wiki/Virtual_method_table

- mary January 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I believe, an error would be thrown "pure virtual method called
"..

check this one: http://tombarta.wordpress.com/2008/07/10/gcc-pure-virtual-method-called/

- shoushou January 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a pure virtual function is that

- anon October 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is usually safe to call any member function from within a constructor because the object has been completely set up (virtual tables have been initialized and so on) prior to the execution of the first line of user code. However, it is potentially unsafe for a member function to call a virtual member function for an abstract base class during construction or destruction.
Constructors can call virtual functions. When virtual functions are called, the function invoked is the function defined for the constructor's own class (or inherited from its bases). The following example shows what happens when a virtual function is called from within a constructor:
// specl_calling_virtual_functions.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
class Base
{
public:
Base(); // Default constructor.
virtual void f(); // Virtual member function.
};

Base::Base()
{
cout << "Constructing Base sub-object\n";
f(); // Call virtual member function
} // from inside constructor.

void Base::f()
{
cout << "Called Base::f()\n";
}

class Derived : public Base
{
public:
Derived(); // Default constructor.
void f(); // Implementation of virtual
}; // function f for this class.

Derived::Derived()
{
cout << "Constructing Derived object\n";
}

void Derived::f()
{
cout << "Called Derived::f()\n";
}

int main()
{
Derived d;
}
When the preceding program is run, the declaration Derived d causes the following sequence of events:
The constructor for class Derived (Derived::Derived) is called.
Prior to entering the body of the Derived class's constructor, the constructor for class Base (Base::Base) is called.
Base::Base calls the function f, which is a virtual function. Ordinarily, Derived::f would be called because the object d is of type Derived. Because the Base::Base function is a constructor, the object is not yet of the Derived type, and Base::f is called.

- Simran June 19, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More