[Cpp]
Create base class base
Create base class base [cpp]
# Ifndef _ base_h
# Define _ base_h
Class base
{
Public:
Base (int a, int B );
~ Base ();
Public:
Int m;
Int n;
};
# Endif
# Ifndef _ base_h
# Define _ base_h
Class base
{
Public:
Base (int a, int B );
~ Base ();
Public:
Int m;
Int n;
};
# Endif [cpp]
* Base. cpp ***************
* ************* Base. cpp ***************** [cpp] view plaincopyprint?
# Include "stdafx. h"
# Include "base. h"
Base: base (int a, int B)
{
M =;
N = B;
Printf ("gou zao ji lei \ n ");
}
Base ::~ Base ()
{
Printf ("xi gou ji lei \ n ");
}
# Include "stdafx. h"
# Include "base. h"
Base: base (int a, int B)
{
M =;
N = B;
Printf ("gou zao ji lei \ n ");
}
Base ::~ Base ()
{
Printf ("xi gou ji lei \ n ");
}
************
Create a subclass frombase to inherit the base class base
[Cpp]
# Ifndef _ use_h
# Define _ use_h
# Include "base. h"
Class frombase: public base
{
Public:
Frombase (int q, int w, int e, int r );
~ Frombase ();
Public:
Int x;
Int y;
};
# Endif
# Ifndef _ use_h
# Define _ use_h
# Include "base. h"
Class frombase: public base
{
Public:
Frombase (int q, int w, int e, int r );
~ Frombase ();
Public:
Int x;
Int y;
};
# Endif
* *********** Frombase. cpp ************
[Cpp]
# Include "stdafx. h"
# Include "use. h"
Frombase: frombase (int q, int w, int e, int r): base (e, r)
{
X = q;
Y = w;
}
Frombase ::~ Frombase ()
{
Printf ("xi gou frombase \ n ");
}
# Include "stdafx. h"
# Include "use. h"
Frombase: frombase (int q, int w, int e, int r): base (e, r)
{
X = q;
Y = w;
}
Frombase ::~ Frombase ()
{
Printf ("xi gou frombase \ n ");
}
C ++ console program test 11
[Cpp]
# Include "stdafx. h"
# Include "base. h"
# Include "use. h"
// Int _ tmain (int argc, _ TCHAR * argv [])
Int main ()
{
Int aa = 1;
Int bb = 2;
Int cc = 3;
Int dd = 4;
Base * _ base;
Frombase * _ frombase;
_ Base = (base *) new frombase (aa, bb, cc, dd );
Printf ("% d \ n", _ base-> m, _ base-> n );
Delete _ base;
Getchar ();
Return 0;
}
Execution result ::
# Include "stdafx. h"
# Include "base. h"
# Include "use. h"
// Int _ tmain (int argc, _ TCHAR * argv [])
Int main ()
{
Int aa = 1;
Int bb = 2;
Int cc = 3;
Int dd = 4;
Base * _ base;
Frombase * _ frombase;
_ Base = (base *) new frombase (aa, bb, cc, dd );
Printf ("% d \ n", _ base-> m, _ base-> n );
Delete _ base;
Getchar ();
Return 0;
}
Execution result: construct base class base
Build a subclass frombase
3 4
Destructor base class
C ++ console program test 22
[Cpp]
# Include "stdafx. h"
# Include "base. h"
# Include "use. h"
Int main ()
{
Int aa = 1;
Int bb = 2;
Int cc = 3;
Int dd = 4;
Base * _ base;
Frombase * _ frombase;
_ Base = (base *) new frombase (aa, bb, cc, dd );
Printf ("% d \ n", _ base-> m, _ base-> n );
_ Frombase = (frombase *) _ base;
Printf ("% d \ n", _ frombase-> m, _ frombase-> n, _ frombase-> x, _ frombase-> y );
Delete _ frombase;
Getchar ();
Return 0;
}
Execution result :::
# Include "stdafx. h"
# Include "base. h"
# Include "use. h"
Int main ()
{
Int aa = 1;
Int bb = 2;
Int cc = 3;
Int dd = 4;
Base * _ base;
Frombase * _ frombase;
_ Base = (base *) new frombase (aa, bb, cc, dd );
Printf ("% d \ n", _ base-> m, _ base-> n );
_ Frombase = (frombase *) _ base;
Printf ("% d \ n", _ frombase-> m, _ frombase-> n, _ frombase-> x, _ frombase-> y );
Delete _ frombase;
Getchar ();
Return 0;
}
Execution result: construct base class base
Build a subclass frombase
3 4
3 4 1 2
Destructor subclass frombase
Destructor base class
Summary: When the subclass object is directly released:
Call the destructor of the subclass before calling the destructor of the base class.
When the subclass object is forcibly converted to the base class type:
Directly call the destructor of the base class and ignore the destructor of the subclass.