Union, struct, and clas in C ++

xiaoxiao2021-03-05  23

Union, struct, and clas in C

Version 1.1

All written in this article are different in C , not C, because both Union or Stuct does not operate, and no access control concept.

These days have not very busy working, just open Bruce Eckel's Thinking In C Volume 1.

Seeing Chapter 7 About Union This section, union also found that Union is similar to Class, 咦, how can I find it when I look at it, is it just that I just took the horse? Also perhaps this book is really a good, it is impossible for it that I can all experience when I read it, so I have a new experience every time I read.

Union, struct and clas are indeed a lot of similar usage. This is the title of reading, and I will write down.

Here, it is still an example.

First define the following three abstract types.

Union Uniona

{

Int m_iint;

CHAR M_CCHAR;

Int getint () {

Initializepara ();

}

PUBLIC:

Uniona () {

Initializepara ();

}

~ uniona () {

Cout << "uniona :: ~ uniona ()" << endl;

}

PUBLIC:

Char getchar () {

Return m_cchar;

}

Private:

VoidinitiZepara () {

m_iint = 0;

m_cchar = 'a';

}

}

Struct structb

{

Int m_iint;

CHAR M_CCHAR;

Int getint () {

Initializepara ();

}

PUBLIC:

Structb () {

Initializepara ();

}

~ structb () {

Cout << "structb :: ~ structb ()" << endl;

}

PUBLIC:

Char getchar () {

Return m_cchar;

}

Private:

VoidinitiZepara () {

m_iint = 0;

m_cchar = 'a';

}

}

Class ClassC

{

Int m_iint;

CHAR M_CCHAR;

Int getint () {

Initializepara ();

}

PUBLIC:

Classc () {

Initializepara ();

}

~ Classc () {

COUT << "Classc :: ~ classc ()" << endl;

}

PUBLIC:

Char getchar () {

Return m_cchar;

}

Private:

VoidinitiZepara () {

m_iint = 0;

m_cchar = 'a';

}

}

1.

All Union, Struct and Class are like objects, define member data and member functions.

2.

Union and Struct The default member is public, while the Class default is private.

3.

Union can neither inherit, and cannot be inherited, while Struct and Class can be used to inherit and inherited.

Such examples:

/ * // Error

Class ClassSuba: Public UNIONA {

}

* /

/ * // Error

Union UnionSuba: Public UNIONA

{

}

* /

Struct Structsubb: Public Structb: PUBLIC STRUCTB

{

}

Class ClassSubc: Public Classc

{

}

Struct StructSubc: Public Classc

{

}

Class ClassSubb: Public StructB

{

}

4.

Different spaces, such as the above example, int m_iint; and char m_cchar; share the same space, the space size is one of them (in the 32-bit machine, INT is 4 bytes). Class and struct are not.

The above is a summary of these three aspects.

(Finish)

About this article: This version is Version 1.1, completed in 2005.03.12. You can find the original text in my personal homepage and blog. If there is any comments or suggestions, I hope to give me, thank you! J

Home: http://zijinshi.cn

Blog: http://blog.9cbs.net/zijinshi,

Email: frank@zijinshi.cn

Frank

2005.03.11 nights - 2005.03.12

转载请注明原文地址:https://www.9cbs.com/read-37112.html

New Post(0)