Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
Cpp - Virtual Destructors       Share
2008-11-03 |  MalathiVanaraj  | Viewed: 1560  |    1

Destructors :
Destructor is a member function,which gets called when the object goes out of scope.
That is all cleanups and final step of class destruction is done in destructors
Destruction is done in the following order,
1.Derived class destructor
2.Base calss destructor

Virtual Destructors :
When a destructor is defined in both base and derived classes, during a function call,
the destructor in derived class gets executed and not the one defined in Base class.
In order to execute both base and derived destructors, the destructor in base class must be declared as virtual.


class base
{
public:
    base()
    {
        cout<<"Constructor : Base" <<endl;
    }
    virtual ~base()  //Base class destructor is defined as virtual
    {
        cout<<"Destructor  : Base" <<endl;
    }
};

class derived :public base
{
public:
    derived()
    {
        cout<<"Constructor : Derived" <<endl;
    }
   ~derived()
    {
        cout<<"Destructor  : Derived" <<endl;
    }
};

int main()
{
    base *val=new derived();
    delete val;
}

Output :
 Constructor : Base
 Constructor : Derived
 Destructor  : Derived
 Destructor  : Base
 
Note :
  If the base class destructor is not declared as virtual, only the derived class destructor will be called.
 


Latest topics
The Preprocessor  Viewed: 303
Type Casting  Viewed: 306
What is conversion constructor?  Viewed: 311
Input/Output Library  Viewed: 314
Definition of OOP:  Viewed: 315
Objects  Viewed: 315
What is Multiple Inheritance?  Viewed: 321
What is Message passing  Viewed: 327
Differentiate between the message and method?  Viewed: 332
What are C++ storage classes?  Viewed: 333

Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software
Tech: C ,Cpp
Send Mail: jmcdoss_05



Related topics
Cpp - c++ interview tips
Cpp - c++ Destructors
What is Encapsulation?
What is Data Abstraction?
What is Inheritance?
What is Polymorphism?
What is Message passing
What is Extensibility?
What is Persistence?
What is Delegation and Genericity?
What is Multiple Inheritance?
Basic concepts of OOPS and Structure of C++ program
what is memory leak?how to avoid that?
When are copy constructors called?
What are all the implicit member functions of the class?

Related References
cpp - pointers in c++
cpp - how to use dll file in the c++ program
cpp - creating and linking dll files to c++ program
cpp - append c++
fibonacci series - c++ - cpp
c++ - factorial sample program
binary search cpp program , c++ samples and tutorials
cpp lexicographical_compare sample program
reversecompare cpp example

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us