C ++ subclass Structure

Source: Internet
Author: User

[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.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.