Amazon Interview Question
Software Engineer / DevelopersIf Base class destructor is declared virtual, then there's no memory leak. The destructor for derived class would be called first then Base class destructor. You should use whatever compiler you're familiar with and play around with this examply. It's actually a very popular C++ question.
There is also a compile time error:-
The default constructor is not present. So, when Class B's constructor tries to call Class A's default constructor, it finds none.
So, either we should provide a separate default constructor for Class A or we assign a default value to the single parameter constructor.
Did Amazon mail you this code after your 2nd phone interview? This code would leak memory. Base class' destructor has to be virtual.
- Khoa April 25, 2006