C++中声明类的class与声明结构体的struct关键字详解

2025-05-27 0 19

class
class 关键字声明型或定义型的对象。
语法

?

1

2

3

4

5

6
[template-spec]

class [ms-decl-spec] [tag [: base-list ]]

{

member-list

} [declarators];

[ class ] tag declarators;

参数
template-spec
可选模板说明。
ms-decl-spec
可选存储说明有关更多信息
tag
给定于型名称。在范围内的标记成为了保留字。标志是可选项。如果省略,定义匿名
base-list
派生其成员的或结构的可选列表。
member-list
成员列表。
declarators
指定型一个或多个实例名称的声明符列表。如果的所有数据成员是 public,声明符可以包含初始值设定项列表。

使用举例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68
// 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 关键字定义结构型和/或结构型的变量。

?

1

2

3

4

5
[template-spec] struct[ms-decl-spec] [tag [: base-list ]]

{

member-list

} [declarators];

[struct] tag declarators;

参数
与class的参数相同,可以参照上面的。
备注
结构型是用户定义的复合型。 它由可具有不同型的字段或成员构成。
C++ 中,结构与相同,只不过其成员默认为 public。
使用结构
在 C 中,你必须显式使用 struct 关键字来声明结构。 在 C++ 中,你不需要在定义该型之后使用 struct 关键字。
可以选择在定义结构型时,通过在右大括号和分号之间放置一个或多个逗号分隔的变量名称来声明变量。
可以初始化结构变量。 每个变量的初始化必须括在大括号中。
有关相关信息,请参阅 class、union 和 enum。
示例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34
#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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 C++中声明类的class与声明结构体的struct关键字详解 https://www.kuaiidc.com/75029.html

相关文章

发表评论
暂无评论