C + + Basics Comb--class, Struct, Union

Source: Internet
Author: User

C + + After studying for a while, I found that I had forgotten the most basic things, strcut (struct), Union (Union) I learned a bunch of things but forgot the two most basic;

Now you have a good time to re-learn the things here;

First, class (classes)

Class is used to define objects;

By default the member is private (private);

Members are not necessarily stored in the order of Declaration;

Ii. Strcut (structural body)

The structure is used to define the process or structure (and the combination of a bunch of attributes);

Members are public by default;

Members are stored in memory in the order they are declared, but not necessarily consecutively;

Iii. Union (Consortium)

A consortium (also known as a common body) is used to define certain special structures;

All members share a piece of memory, the size of the memory and the maximum decision of the length of the member;

Member variables are stored in memory in the order in which they are declared, and the memory is contiguous;

1#include <iostream>2 3 using namespacestd;4 5 Union IP_Address6 {7UnsignedintIP;8UnsignedCharr[5];9 };Ten  One intMain () A { - ip_address ip1; -Ip1.ip =0x4544434241; thecout << Ip1.ip <<Endl; -cout << ip1.r[0] <<Endl; -cout << ip1.r[1] <<Endl; -cout << ip1.r[2] <<Endl; +cout << ip1.r[3] <<Endl; -cout << ip1.r[4] <<Endl; +     return 0; A}
View Code

C + + Basics Comb--class, Struct, Union

Related Article

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.