destructors,constructors,virtual functions

when an object of child class is created,first the parent class constructor is executed and then the child class.I hav a doubt as to which status function will be called n y..and also which destructor is called first.Thnx in advance for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream.h> 
class Parent 
{ 
public: 
 Parent(){Status();} 
 virtual ~Parent() { Status();} 
 virtual void Status(){cout<<"Parent ";} 
}; 
class Child: public Parent 
{ 
public: 
 Child(){Status();} 
 virtual ~Child() { Status();} 
 virtual void Status(){cout<<"Child ";} 
}; 
void main() 
{ 
 Child c; 
} 
Constructors are called in order from most base to most derived class. Destructors are backward.

In your example Status() function from Parent object would be called by Parent constructor then Childs Status() function by Child constructor. Then Childs Status() function by Child destructor and then Parent Status() by Parent destructor

You could see that if you run your code (after fixing several errors in it)
Last edited on
Topic archived. No new replies allowed.