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++中結(jié)構(gòu)體的類型定義和初始化以及變量引用
- C++動態(tài)分配和撤銷內(nèi)存以及結(jié)構(gòu)體類型作為函數(shù)參數(shù)
- C++結(jié)構(gòu)體struct和類class區(qū)別詳解
- C++結(jié)構(gòu)體與類指針知識點(diǎn)總結(jié)
- C++關(guān)于類結(jié)構(gòu)體大小和構(gòu)造順序,析構(gòu)順序的測試詳解
- C++類結(jié)構(gòu)體與json相互轉(zhuǎn)換
- C++結(jié)構(gòu)體與類的區(qū)別詳情
- C++?中的?JSON?序列化和反序列化及結(jié)構(gòu)體與枚舉類型的處理方法
- C++ 中類(class)和結(jié)構(gòu)體(struct)的區(qū)別
相關(guān)文章
如何使用arm-none-eabi-gcc編譯器搭建STM32的Vscode開發(fā)環(huán)境
這篇文章主要介紹了使用arm-none-eabi-gcc編譯器搭建STM32的Vscode開發(fā)環(huán)境,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
C++項(xiàng)目基于HuffmanTree實(shí)現(xiàn)文件的壓縮與解壓縮功能
這篇文章主要介紹了C++項(xiàng)目基于HuffmanTree實(shí)現(xiàn)文件的壓縮與解壓縮功能,本文給大家提到文件壓縮的概念介紹及壓縮方法,通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
方陣順時(shí)針旋轉(zhuǎn)的實(shí)現(xiàn)代碼
以下是關(guān)于方陣順時(shí)針旋轉(zhuǎn)的實(shí)現(xiàn)代碼。需要的朋友參考下2013-05-05
C++?Protobuf實(shí)現(xiàn)接口參數(shù)自動校驗(yàn)詳解
用C++做業(yè)務(wù)發(fā)開的同學(xué)是否還在不厭其煩的編寫大量if-else模塊來做接口參數(shù)校驗(yàn)?zāi)??今天,我們就模擬Java里面通過注解實(shí)現(xiàn)參數(shù)校驗(yàn)的方式來針對C++?protobuf接口實(shí)現(xiàn)一個(gè)更加方便、快捷的參數(shù)校驗(yàn)自動工具,希望對大家有所幫助2023-04-04

