C++中聲明類(lèi)的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字詳解
class
class 關(guān)鍵字聲明類(lèi)類(lèi)型或定義類(lèi)類(lèi)型的對(duì)象。
語(yǔ)法
[template-spec]
class [ms-decl-spec] [tag [: base-list ]]
{
member-list
} [declarators];
[ class ] tag declarators;
參數(shù)
template-spec
可選模板說(shuō)明。
ms-decl-spec
可選存儲(chǔ)類(lèi)說(shuō)明有關(guān)更多信息
tag
給定于類(lèi)的類(lèi)型名稱(chēng)。在類(lèi)范圍內(nèi)的標(biāo)記成為了保留字。標(biāo)志是可選項(xiàng)。如果省略,定義匿名類(lèi)。
base-list
此類(lèi)派生其成員的類(lèi)或結(jié)構(gòu)的可選列表。
member-list
類(lèi)成員列表。
declarators
指定類(lèi)類(lèi)型一個(gè)或多個(gè)實(shí)例名稱(chēng)的聲明符列表。如果類(lèi)的所有數(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)類(lèi)型和/或結(jié)構(gòu)類(lèi)型的變量。
[template-spec] struct[ms-decl-spec] [tag [: base-list ]]
{
member-list
} [declarators];
[struct] tag declarators;
參數(shù)
與class的參數(shù)相同,可以參照上面的。
備注
結(jié)構(gòu)類(lèi)型是用戶(hù)定義的復(fù)合類(lèi)型。 它由可具有不同類(lèi)型的字段或成員構(gòu)成。
在 C++ 中,結(jié)構(gòu)與類(lèi)相同,只不過(guò)其成員默認(rèn)為 public。
使用結(jié)構(gòu)
在 C 中,你必須顯式使用 struct 關(guān)鍵字來(lái)聲明結(jié)構(gòu)。 在 C++ 中,你不需要在定義該類(lèi)型之后使用 struct 關(guān)鍵字。
可以選擇在定義結(jié)構(gòu)類(lèi)型時(shí),通過(guò)在右大括號(hào)和分號(hào)之間放置一個(gè)或多個(gè)逗號(hào)分隔的變量名稱(chēng)來(lái)聲明變量。
可以初始化結(jié)構(gòu)變量。 每個(gè)變量的初始化必須括在大括號(hào)中。
有關(guān)相關(guān)信息,請(qǐng)參閱 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ù)使用的超級(jí)學(xué)習(xí)教程
欄 目:C語(yǔ)言
本文標(biāo)題:C++中聲明類(lèi)的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字詳解
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2538.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見(jiàn)的關(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ù)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?


