Tuesday, September 22, 2009

What do you mean by virtual destructor?

What do you mean by virtual destructor?
Just like ordinary virtual functions destructors can also be declared as virtual, whereas constructor cannot be Virtual. When a derived object pointed is deleted, destructors of all its base classes as well as destructor of derived classes are involved if we declare base class pointer as virtual.
But if the destructor in base class is non-virtual then only the destructor of the base class is invoked when the base class pointer pointing to the derived class object is deleted.

//Code of virtual destructor

#include<iostream.h>
#include<string.h>
class parent
{
protected:
char*name;
public:
parent (char*x)
{
name=new char[strlen(x)+1];
strcpy(name x);
}
virtual void show()
{
cout < <"parents name:" < <name;
}
virtual ~parent()
{
delete name;
cout<<"parent destroyed"<<endl;
}
};
class child:public parent
{
private:
char*name;
public:
child(char*x1,char x2):parent(x1)
{
name=new char[strlen(x2)+1];
strcpy(name,x2);
}
void show()
{
cout<<endl<<"childs name:"<<name;
}
~child()
{
delete name;
cout<<endl<<"child destroyed";
}
};
void main()
{
parent *p1;
p1=new parent("parent");
p1->show();
delete p1;
p1=new child (parent from child class:."child");
p1->parent::show();
delete p1;
getch();
}
Output
Parent name: parent
Parent destroyed
Child name: child
Parent name: parent from child class
Child destroyed
Parent destroyed

If we remove virtual keyword in the definition of destructor of base class, the second last output line (“child destroyed”) is not obtained virtual function used in following situation:

It is used when one class needs to delete object of derived class that are addresses by the base class pointer and invoked a base class destructor to release resources allocated to it.

When delete operation is performed on an object by a pointer or reference the program will first call the object destructor instead of the destructor associated with the pointer or reference type if the destructor is defined as virtual.“this” pointer

C++ uses a unique keyword called ‘this’ to represent an object that invokes a member function
This is a pointer that points to the object for which this function was called.
-for e.g. the function call A.max() will set the pointer this to the address of the object A
This unique pointer is automatically passed to a member function when it is called. The pointer ‘this ’ acts as an implicit argument to call the member functions
This pointer refers to an object that currently invokes a member functions
Syntax:
class test
{
Int data;
public:
func(){..;}
func()
{
this->data;
this->func();
}
};
Complex add(complex c2)
{
Complex t;
t.real=real+c2.real;
t.imag=imag+c2.imag;
return *this;
}

Therefore, this pointer can be used to return objects
For e.g.
#include<iostream.h>
Class person
{
Int age;
Public:
person(int a)
{age=a;}
Void disp;ay()
{Cout<<age;}
Person greater(person&);
};
Person person::greater(person &x)
{

If(age,x.age)
return x;
else
return *this;
}
void main()
{
person per(15), per(13);
person p=per.greater(pet);
cout <<”the greater age is :”;
p.display();
}

Output
The greater age is:15

No comments:

Post a Comment