9久久伊人精品综合,亚洲一区精品视频在线,成 人免费va视频,国产一区二区三区黄网,99国产精品永久免费视频,亚洲毛片多多影院,精品久久久无码人妻中文字幕,无码国产欧美一区二区三区不卡
學習啦>學習英語>專業英語>計算機英語>

c中class的用法

時間: 長思709 分享

  c中class的用法的用法你知道嗎?下面小編就跟你們詳細介紹下c中class的用法的用法,希望對你們有用。

  c中class的用法的用法如下:

  Struct和Class的區別

  今天這篇博文主要講解在C++中關鍵字struct和class的區別。這篇博文,將會系統的將這兩個關鍵字的不同面進行詳細的講解。

  從語法上來講,class和struct做類型定義時只有兩點區別:

  1.默認繼承權限,如果不指定,來自class的繼承按照private繼承處理,來自struct的繼承按照public繼承處理;

  2.成員的默認訪問權限。class的成員默認是private權限,struct默認是public權限。以上兩點也是struct和class最基本的差別,也是最本質的差別;

  但是在C++中,struct進行了擴展,現在它已經不僅僅是一個包含不同數據類型的數據結構了,它包括了更多的功能。

  Struct能包含成員函數嗎?

  是的,答案是肯定的。現在就讓我寫一段代碼驗證一下:

  復制代碼 代碼如下:

  /*

  ** FileName : StructAndClassDiffDemo

  ** Author : Jelly Young

  ** Date : 2013/12/7

  ** Description : More information, please go to http://www.jb51.net

  */

  #include <iostream>

  using namespace std;

  struct Test

  {

  int a;

  int getA()

  {

  return a;

  }

  void setA(int temp)

  {

  a = temp;

  }

  };

  int main(int argc, char* argv[])

  {

  Test testStruct;

  testStruct.setA(10);

  cout<<"Get the value from struct:"<<testStruct.getA()<<endl;

  Test *testStructPointer = new Test;

  testStructPointer->setA(20);

  cout<<"Get the value from struct again:"<<testStructPointer->getA()<<endl;

  delete testStructPointer;

  return 0;

  }

  以上的代碼會很正確的運行,是的;沒錯,struct能包含成員函數的。

  Struct有自己的構造函數嗎?

  是的,可以的。看以下測試代碼:

  復制代碼 代碼如下:

  /*

  ** FileName : StructAndClassDiffDemo

  ** Author : Jelly Young

  ** Date : 2013/12/7

  ** Description : More information, please go to http://www.jb51.net

  */

  #include <iostream>

  using namespace std;

  struct Test

  {

  int a;

  Test()

  {

  a = 100;

  }

  int getA()

  {

  return a;

  }

  void setA(int temp)

  {

  a = temp;

  }

  };

  int main(int argc, char* argv[])

  {

  Test testStruct;

  testStruct.setA(10);

  cout<<"Get the value from struct:"<<testStruct.getA()<<endl;

  Test *testStructPointer = new Test;

  testStructPointer->setA(20);

  cout<<"Get the value from struct again:"<<testStruct.getA()<<endl;

  delete testStructPointer;

  // test the constructor

  Test testConstructor;

  cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl;

  return 0;

  }

  Struct可以有析構函數么?

  讓我來驗證一下:

  復制代碼 代碼如下:

  /*

  ** FileName : StructAndClassDiffDemo

  ** Author : Jelly Young

  ** Date : 2013/12/7

  ** Description : More information, please go to http://www.jb51.net

  */

  #include <iostream>

  using namespace std;

  struct Test

  {

  int a;

  Test()

  {

  a = 100;

  }

  int getA()

  {

  return a;

  }

  void setA(int temp)

  {

  a = temp;

  }

  ~Test()

  {

  cout<<"Destructor function called."<<endl;

  }

  };

  int main(int argc, char* argv[])

  {

  Test testStruct;

  testStruct.setA(10);

  cout<<"Get the value from struct:"<<testStruct.getA()<<endl;

  Test *testStructPointer = new Test;

  testStructPointer->setA(20);

  cout<<"Get the value from struct again:"<<testStruct.getA()<<endl;

  delete testStructPointer;

  // test the constructor

  Test testConstructor;

  cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl;

  return 0;

  }

  是的,完全支持析構函數。

  Struct支持繼承么?

  再讓我寫代碼驗證一下:

  復制代碼 代碼如下:

  /*

  ** FileName : StructAndClassDiffDemo

  ** Author : Jelly Young

  ** Date : 2013/12/7

  ** Description : More information, please go to http://www.jb51.net

  */

  #include <iostream>

  using namespace std;

  struct A

  {

  int a;

  A()

  {

  a = 10;

  }

  void print()

  {

  cout<<"I am from A"<<endl;

  }

  };

  struct B : A

  {

  int b;

  B()

  {

  a = 30; // set a to 30

  b = 20;

  }

  /*void print()

  {

  cout<<"I am from B"<<endl;

  }*/

  };

  int main(int argc, char* argv[])

  {

  B b1;

  cout<<b1.a<<endl;

  cout<<b1.b<<endl;

  b1.print();

  A a1;

  cout<<a1.a<<endl;

  a1.print();

  return 0;

  }

  運行上述代碼,struct支持繼承。

  Struct支持多態么?

  寫代碼測試一下便知:

  復制代碼 代碼如下:

  /*

  ** FileName : StructAndClassDiffDemo

  ** Author : Jelly Young

  ** Date : 2013/12/7

  ** Description : More information, please go to http://www.jb51.net

  */

  #include <iostream>

  using namespace std;

  struct A

  {

  virtual void print() = 0;

  };

  struct B : A

  {

  void print()

  {

  cout<<"I am from B"<<endl;

  }

  };

  struct C : A

  {

  void print()

  {

  cout<<"I am from C"<<endl;

  }

  };

  int main(int argc, char* argv[])

  {

  A *a1;

  B *b1 = new B;

  C *c1 = new C;

  a1 = b1;

  a1->print(); // call B, not A

  a1 = c1;

  a1->print(); // call C, not A

  return 0;

  }

  Struct支持Private、Protected和Public關鍵字么?

  復制代碼 代碼如下:

  /*

  ** FileName : StructAndClassDiffDemo

  ** Author : Jelly Young

  ** Date : 2013/12/7

  ** Description : More information, please go to http://www.jb51.net

  */

  #include <iostream>

  using namespace std;

  struct A

  {

  private:

  int b;

  protected:

  int c;

  public:

  A()

  {

  b = 10;

  c = 20;

  d = 30;

  }

  int d;

  };

  struct B : A

  {

  void printA_C()

  {

  cout<<A::c<<endl;

  };

  // private member can not see

  /*void printA_B()

  {

  cout<<A::b<<endl;

  }*/

  void printA_D()

  {

  cout<<A::d<<endl;

  }

  };

  int main(int argc, char* argv[])

  {

  A a1;

  B b1;

  // private member can not see

  //cout<<a1.b<<endl;

  // protected member can not see

  //cout<<a1.c<<endl;

  // public member can see

  cout<<a1.d<<endl;

  return 0;

  }

537098 主站蜘蛛池模板: 亚洲AV无码成H人动漫无遮挡 | 九九热久久这里全是精品| 亚洲欧美人成人让影院| 深夜精品免费在线观看| 日韩一区二区三区一级片| 亚洲av成人午夜福利| 2021国产精品视频网站| 61精品人妻一区二区三区| 国产va免费精品观看| 国内精品视频一区二区三区八戒| 黄色A级国产免费大片视频| 久久人人97超碰精品| 人妻饥渴偷公乱中文字幕| av在线播放观看国产| 亚洲综合伊人久久大杳蕉| 亚洲中文字幕无码专区 | 视频一区二区不中文字幕| 免费VA国产高清大片在线| 国产精品视频午夜福利| 在线播放亚洲成人av| 亚洲国产成人久久综合区| 免费中文熟妇在线影片| 亚洲综合小说另类图片五月天| 亚洲一区二区偷拍精品| 国模精品视频一区二区三区| 性色在线视频精品| 玩弄放荡人妻少妇系列| 97人人添人人澡人人澡人人澡| 777奇米四色成人影视色区| 久久久精品2019中文字幕之3| 成人福利国产午夜AV免费不卡在线| 国产成人无码一区二区三区| 国产精品麻豆中文字幕| 日韩高清在线亚洲专区不卡| 久久国产精品波多野结衣| 男女啪啪高潮激烈免费版| 亚洲日韩欧美丝袜另类自拍 | 被灌满精子的少妇视频| 宅男久久精品国产亚洲av麻豆| 野外做受三级视频| 一区二区三区午夜福利院|