C++中聲明類的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字詳解
class
class 關(guān)鍵字聲明類類型或定義類類型的對象。
語法
[template-spec] class [ms-decl-spec] [tag [: base-list ]] { member-list } [declarators]; [ class ] tag declarators;
參數(shù)
template-spec
可選模板說明。
ms-decl-spec
可選存儲類說明有關(guān)更多信息
tag
給定于類的類型名稱。在類范圍內(nèi)的標(biāo)記成為了保留字。標(biāo)志是可選項(xiàng)。如果省略,定義匿名類。
base-list
此類派生其成員的類或結(jié)構(gòu)的可選列表。
member-list
類成員列表。
declarators
指定類類型一個(gè)或多個(gè)實(shí)例名稱的聲明符列表。如果類的所有數(shù)據(jù)成員是 public,聲明符可以包含初始值設(shè)定項(xiàng)列表。
使用舉例
// class.cpp // compile with: /EHsc // Example of the class keyword // Exhibits polymorphism/virtual functions. #include <iostream> #include <string> #define TRUE = 1 using namespace std; class dog { public: dog() { _legs = 4; _bark = true; } void setDogSize(string dogSize) { _dogSize = dogSize; } virtual void setEars(string type) // virtual function { _earType = type; } private: string _dogSize, _earType; int _legs; bool _bark; }; class breed : public dog { public: breed( string color, string size) { _color = color; setDogSize(size); } string getColor() { return _color; } // virtual function redefined void setEars(string length, string type) { _earLength = length; _earType = type; } protected: string _color, _earLength, _earType; }; int main() { dog mongrel; breed labrador("yellow", "large"); mongrel.setEars("pointy"); labrador.setEars("long", "floppy"); cout << "Cody is a " << labrador.getColor() << " labrador" << endl; }
struct
struct 關(guān)鍵字定義結(jié)構(gòu)類型和/或結(jié)構(gòu)類型的變量。
[template-spec] struct[ms-decl-spec] [tag [: base-list ]] { member-list } [declarators]; [struct] tag declarators;
參數(shù)
與class的參數(shù)相同,可以參照上面的。
備注
結(jié)構(gòu)類型是用戶定義的復(fù)合類型。 它由可具有不同類型的字段或成員構(gòu)成。
在 C++ 中,結(jié)構(gòu)與類相同,只不過其成員默認(rèn)為 public。
使用結(jié)構(gòu)
在 C 中,你必須顯式使用 struct 關(guān)鍵字來聲明結(jié)構(gòu)。 在 C++ 中,你不需要在定義該類型之后使用 struct 關(guān)鍵字。
可以選擇在定義結(jié)構(gòu)類型時(shí),通過在右大括號和分號之間放置一個(gè)或多個(gè)逗號分隔的變量名稱來聲明變量。
可以初始化結(jié)構(gòu)變量。 每個(gè)變量的初始化必須括在大括號中。
有關(guān)相關(guān)信息,請參閱 class、union 和 enum。
示例
#include <iostream> using namespace std; struct PERSON { // Declare PERSON struct type int age; // Declare member types long ss; float weight; char name[25]; } family_member; // Define object of type PERSON struct CELL { // Declare CELL bit field unsigned short character : 8; // 00000000 ???????? unsigned short foreground : 3; // 00000??? 00000000 unsigned short intensity : 1; // 0000?000 00000000 unsigned short background : 3; // 0???0000 00000000 unsigned short blink : 1; // ?0000000 00000000 } screen[25][80]; // Array of bit fields int main() { struct PERSON sister; // C style structure declaration PERSON brother; // C++ style structure declaration sister.age = 13; // assign values to members brother.age = 7; cout << "sister.age = " << sister.age << '\n'; cout << "brother.age = " << brother.age << '\n'; CELL my_cell; my_cell.character = 1; cout << "my_cell.character = " << my_cell.character; } // Output: // sister.age = 13 // brother.age = 7 // my_cell.character = 1
上一篇:完全掌握C++編程中構(gòu)造函數(shù)使用的超級學(xué)習(xí)教程
欄 目:C語言
本文標(biāo)題:C++中聲明類的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字詳解
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2538.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?