C++基礎梳理--Class、Struct、Union

來源:互聯網
上載者:User

標籤:

C++學習一段時間後,反過頭來看我發現我忘了一下最基礎的東西;strcut(結構體),union(聯合體)我學會了類的一堆東西卻忘了這兩個最基礎的;

現在就好好的重新學習一下這裡的東西;

一、Class(類)

類用以定義對象;

預設情況下成員私人(private);

成員不一定按聲明順序儲存;

二、strcut(結構體)

結構體用以定義過程或者結構(及一堆屬性的結合);

預設情況下成員公有(public);

成員按聲明順序在記憶體中儲存,但不一定是連續的;

三、union(聯合體)

聯合體(又叫共用體)用以定義用於定義一些特殊結構;

所有成員共用一塊記憶體,記憶體的大小與成員中長度的最大的決定;

成員變數按聲明順序在記憶體中儲存,並且記憶體是連續的;

 

 1 #include <iostream> 2  3 using namespace std; 4  5 union ip_address 6 { 7     unsigned int ip; 8     unsigned char r[5]; 9 };10 11 int main()12 {13     ip_address ip1;14     ip1.ip = 0x4544434241;15     cout << ip1.ip << endl;16     cout << ip1.r[0] << endl;17     cout << ip1.r[1] << endl;18     cout << ip1.r[2] << endl;19     cout << ip1.r[3] << endl;20     cout << ip1.r[4] << endl;21     return 0;22 }
View Code

 

C++基礎梳理--Class、Struct、Union

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.