欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

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

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

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

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

如果侵犯了您的權(quán)利,請與我們聯(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)所有