what is virtual destructor

Answer Posted / rock

When a derived class object pointed to by a base class
pointer dynamically is deleted only the base class
destructor is invoked inorder to even invoke derived class
destructor we use virtual destructor.

class shape
{
virtual ~shape(){}
};
class circle:public shape
{
~circle(){}
};
void main()
{
shape *sh = new circle;
delete sh; //both the destructors are invoked
}

If virtual keyword is not added to the base class
destructor only the base class destructor is called.

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

why reinterpret cast is considered dangerous?

1906


Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)

1643


Write a program to sort the number with different sorts in one program ??

1923


What are the 4 main oop principles?

685


What is difference between multiple inheritance and multilevel inheritance?

603






Can you explain polymorphism?

584


Why do we use class?

638


What is the importance of oop?

617


What does it mean when someone says I oop?

587


What is polymorphism and example?

594


What do you mean by variable?

578


What is polymorphism oop?

624


Write a program to compute for numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the following format: Each line contains a student's first name, then one space, then ten quiz scores all on one line. The quiz scores are in whole number and are separated by one space. Your program will take it input from this file and sends it output to a second file. The data in the output file will be exactly the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student's ten quiz scores. Use at least one function that has file streams as all or some of its arguments.

2578


Why do while loop is used?

579


Templates mean

1592