HCL Interview Question for Software Engineer / Developers






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

As mentioned, the need exists if objects will be tracked by use of pointers of the type of the base class.

A pointer to a base class object is of the type of the base class, but can refer to derived classes as well.

When you use that pointer to destroy/delete an object derived from the base class, and the pointer is to a base class object, then the destructor for the base class is called.

That destructor only knows about the base object components variables and memory allocations. It doesn't know about special variables instantiated in derived classes, nor does it even know what the derived class is.

By declaring the destructor virtual, the compiler will require the derived class declare its own destructor, AND, the type of the object pointed to by the generic base class pointer will be determined at run time rather than at compile time.

Thus if a base class pointer is used to destroy an object, the execution environement then determines the true type of the dervived class and calls that destructure (whihc must still be responsible and clean up what it should), rather than the base class destructor (which cannot possibly clean up what it doesn't know about).

The use of virtual destructors is a strong help in preventing memory leaks from a program.

- Thomas S November 03, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You need to make the destructor Virtual in case of inheritance in perticularly in following case -

If someone will derive from your class(class BASE),
and if someone will say new Derived, where Derived is derived from your class(BASE),
and if someone will say delete p, where the actual object's type is Derived class but the pointer p's type is your class(BASE).

something like this-

BASE *p;
p = new Derived();
delete p;

- Regs September 12, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The idea is, compiler can understand how much sz should delete for built in types (int, char etc) but whenever your deleting
userdefined types basically it calls destr for that obj.

In above case whenever your deleting dervd obj with base ptr refernce, u need to declare base destr as virtual destr to clean up resources allocated by derived classes (new Derived() ---- calls derived constructor) else it calls only base destr.

- Adepu Srinivas September 12, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

And a catch is that in either the base and derived classes, there need not be any virtual functions present.

- anil September 18, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If a class may serve as a base class for others, its destructor should be virtual
class Base{
char *p;
Base() { p = new char [200]; }
~ Base () {delete [] p; }
//...
};

class Derived : public Base {
char *q;
Derived() { q = new char[300]; }
~Derived() { delete [] q; }
//...
};

void destroy (Base & b) { delete &b; }

int main()
{
Base *pb = new Derived(); //200 + 300 bytes have been allocated

//... meddle with pb
destroy (*pb); //Oops! Base’s destructor is called but not //Derived’s; memory leak.
//Were Base’s destructor virtual, the //correct destructor would be alled.
}

- ITmax.com September 21, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If a class may serve as a base class for others, its destructor should be virtual
class Base{
char *p;
Base() { p = new char [200]; }
~ Base () {delete [] p; }
//...
};

class Derived : public Base {
char *q;
Derived() { q = new char[300]; }
~Derived() { delete [] q; }
//...
};

void destroy (Base & b) { delete &b; }

int main()
{
Base *pb = new Derived(); //200 + 300 bytes have been allocated

//... meddle with pb
destroy (*pb); //Oops! Base’s destructor is called but not //Derived’s; memory leak.
//Were Base’s destructor virtual, the //correct destructor would be alled.
}

- Parthipan September 21, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If a class may serve as a base class for others, its destructor should be virtual
class Base{
char *p;
Base() { p = new char [200]; }
~ Base () {delete [] p; }
//...
};

class Derived : public Base {
char *q;
Derived() { q = new char[300]; }
~Derived() { delete [] q; }
//...
};

void destroy (Base & b) { delete &b; }

int main()
{
Base *pb = new Derived(); //200 + 300 bytes have been allocated

//... meddle with pb
destroy (*pb); //Oops! Base’s destructor is called but not //Derived’s; memory leak.
//Were Base’s destructor virtual, the //correct destructor would be alled.
}


SO Now what will be calling sequece Dervid Dest then base destr or Base destr then derived destr

- Pankaj September 27, 2006 | 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