why is only base destructor being called .

Hi , As in my code .. only the destructor of the base class is being called .
i was assuming that the destructor of the derived class will be called first then the destructor of the base class .. but it is not happning .. i am not able to understand why .. please explain me this . Any help is appreciated .
thanks in advance ..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "stdafx.h"
#include <iostream>
using namespace std; 
class CTestA
{
private:
	int a  ;
public:
	CTestA()  { a = 5 ; cout<<"\n This the testA constructor ";}  
	virtual ~CTestA() { cout<<"\n This the testA destructor  "; }  
	virtual void Print() ; 
};
void CTestA::Print()
{
	cout<<"\n Print value of A = "<<a ;
}

class CTestB : public CTestA
{
private :
	int b ; 
public :
	CTestB() : CTestA()  { b = 10  ; cout << "\n This the constructor of test B " ; }   
	~CTestB() { cout<<"\n This is the destructor of the TestB and the value of b = "<<b ;}
	void Print();
	
};
void CTestB::Print()
{
	cout<<"\n This is the print function of TestB ";
}
//using the multiple inheretance . 
class CTestC : public CTestA
{
private:
	int c ; 
public:
	CTestC() { c = 15 ;  "\n This is the constructor of the Test C "; } 
	~CTestC() {"\n This is the destructor of the CTest C "; }
	void Print() ; 
};
void CTestC::Print()
{
	cout<<"\n This is the print of the CTest C having the value of c = " <<c ;
}
int _tmain(int argc, _TCHAR* argv[])
{
	CTestA *test = new CTestC() ; 
	test->Print();
	delete test ; 
	return 0;
}
The child destructor is being called.

You forgot to actually cout that text:

1
2
	CTestC() { c = 15 ;  "\n This is the constructor of the Test C "; }  // <- missing cout
	~CTestC() {"\n This is the destructor of the CTest C "; } // <-same 
A silly mistake .. thanks Disch .. i don't know till when i am going to make these type of mistakes ... :)
A better way to find out if code is actually running is to put a breakpoint on the line in question.

Debuggers are great.
Thanks Disch .. i will keep that in the mind . . .
Topic archived. No new replies allowed.