Interview Question


Country: India




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

I suppose one question would be why you are attempting to extend a class that was not designed for inheritance (or else it would have been given a virtual destructor). I'm sure there may be situations when you might need to do something like that, but are you sure you need to use inheritance here? Is this an interview question?

- eugene.yarovoi December 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes it was an interview question. And the answer i gave was to put virtual destrucutors in the derived classes and when deleting the object from base pointers; we need to use dynamic_cast to to downcast to correct object and then finally delete the concrete object.

I am not sure what was the intent of this question, was he expecting RTTI or is there any technique.. I am not sure.

But you are right a library of that kind should not be used for inheritance.

- Xyz December 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think question is incomplete, whether this base class has virtual functions or not ? if not then why we need virtual destructor if no polymorphism.

- Guest January 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I hope we need to overload new and delete operator in derived class.

- hyder December 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make a new base class BaseX by inheriting from the third party Base class and Declare destructor of this new BaseX as virtual. Now use new BaseX wherever you need to inherit. This will resolve all your issues.

- Taqreez March 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why do we need to inherit it even? I will create a wrapper class and make the third party's class a member and forward all the functions to that member. When my class goes out of scope, the destructor of the member will be called and it will be properly cleaned up.

- Coder April 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think when you inherit in derived class, do not create pointer object of base class. Always create pointer object of derived one and it will always call the derived class destructor and not base.

- Nickman February 06, 2015 | 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