雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C++中聲明類(lèi)的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字詳解

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎ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ǔ)言

下一篇:Linux c中define的用法小結(jié)

本文標(biāo)題:C++中聲明類(lèi)的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字詳解

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2538.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有