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

2020-01-06 14:29:23王振洲

备注
结构类型是用户定义的复合类型。 它由可具有不同类型的字段或成员构成。
在 C++ 中,结构与类相同,只不过其成员默认为 public。
使用结构
在 C 中,你必须显式使用 struct 关键字来声明结构。 在 C++ 中,你不需要在定义该类型之后使用 struct 关键字。
可以选择在定义结构类型时,通过在右大括号和分号之间放置一个或多个逗号分隔的变量名称来声明变量。
可以初始化结构变量。 每个变量的初始化必须括在大括号中。
有关相关信息,请参阅 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++教程频道。