#include<iostream>
usingnamespace std;
class A{
int i;
int b;
public:
A(){
i=0;
b=0;
cout<<"constructor"<<endl;
}
int printf(){
cout<<i<<" "<<b<<endl;
return 1;
}
~A(){
cout<<"A out of scope"<<endl;
}
};
class B:public A {
int i;
int b;
public:
B(){
i=0;
b=9;
cout<<"constructor of B"<<endl;
}
int printf(){
cout<<i<<" "<<b<<endl;
return 1;
}
~B(){
cout<<"B out of scope"<<endl;
}
};
int main(){
A obj;
obj.printf();
B obj1;
obj1.printf();
cout<<endl;
system("pause");
return 0;
}
#include<iostream>
usingnamespace std;
class A{
public:
A() { cout<<"constructor of A"<<endl; }
~A(){ cout<<"destructor of A"<<endl; }
};
class B:public A {
public:
B() { cout<<"constructor of B"<<endl; }
~B(){ cout<<"destructor of B"<<endl; }
};
int main()
{
A objA;
B objB;
}
constructor of A
constructor of A
constructor of B
destructor of B
destructor of A
destructor of A
These destructors are being called. You have system("pause"); in there. The destructors are only called AFTER system("pause");
Try running your console app in the console instead of double-clicking on the ICON like so:
Microsoft Windows [Version 6.2.8250]
(c) 2012 Microsoft Corporation. All rights reserved.
C:\Users\Stew>cd "C:\Dev\Snippets\Release"
C:\Dev\Snippets\Release>Snippets.exe
constructor of A
constructor of A
constructor of B
destructor of B
destructor of A
destructor of A
C:\Dev\Snippets\Release>