Goldman Sachs Interview Question for Software Engineer / Developers






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

When you call delete BasePtr();, the destructor of the class which is pointed by BasePtr needs to be called. To acheive this, we need the destructor to be virtual.

Without Virtual, the delete BasePtr() will call the class of type BasePtr.

- Ganesh December 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

still don't understand clearly, can anyone help with this.. thanks!

- shoushou January 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

class base
{
...
~base()
}

class derived:public base
{
int *p;
public:
~derived()
{
delete p;
}
};

main()
{
base *b = new derived;
delete b;
}

here b being a base pointer delete b calls base class destructor directly and gets itself deleted thereby never deleting the pointer p. for which memory is alloted during creation. to avoid this if base destructor is virtual. it acalls derived destructor and deletes p.

- VK July 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If destructor is not declared virtual in base class, then when you try to delete a derived class using base class pointer, the destructor of base class is called instead of the derived class destructor. Hence virtual destructor.

- Bikash February 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just to add a note to vk..if we declare the base class destructor as virtual and if try to delete the base class pointer..
The order of execution of destructor in an inherited class during a clean up is like this.
1. Derived class destructor
2. Base class destructor

- JackMaster January 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

read chap 15 of bruce eckel book vol-1(Thinking in C++)

- Anonymous July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

read chap 15 of bruce eckel book vol-1(Thinking in C++)

- lambor July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

class A{
public:
A(){
cout<<"in a \n";
}
virtual ~A(){
cout<<"out A"<<endl;
}
};

class b:public A{

public:
b(){
cout<<"in B\n"<<endl;
}
~b(){
cout<<"out b"<<endl;
}
};


int main(){
A *bc=new b();
// A *a=new A();

delete bc;
getchar();
}

- ridercoder October 21, 2010 | 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