<em id="0a85b"><option id="0a85b"></option></em>

<abbr id="0a85b"></abbr>

      <nobr id="0a85b"></nobr>
        <tr id="0a85b"></tr>
        9久久伊人精品综合,亚洲一区精品视频在线,成 人免费va视频,国产一区二区三区黄网,99国产精品永久免费视频,亚洲毛片多多影院,精品久久久无码人妻中文字幕,无码国产欧美一区二区三区不卡
        學(xué)習(xí)啦>學(xué)習(xí)英語>專業(yè)英語>計(jì)算機(jī)英語>

        c中class的用法

        時間: 長思709 分享

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

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

          Struct和Class的區(qū)別

          今天這篇博文主要講解在C++中關(guān)鍵字struct和class的區(qū)別。這篇博文,將會系統(tǒng)的將這兩個關(guān)鍵字的不同面進(jìn)行詳細(xì)的講解。

          從語法上來講,class和struct做類型定義時只有兩點(diǎn)區(qū)別:

          1.默認(rèn)繼承權(quán)限,如果不指定,來自class的繼承按照private繼承處理,來自struct的繼承按照public繼承處理;

          2.成員的默認(rèn)訪問權(quán)限。class的成員默認(rèn)是private權(quán)限,struct默認(rèn)是public權(quán)限。以上兩點(diǎn)也是struct和class最基本的差別,也是最本質(zhì)的差別;

          但是在C++中,struct進(jìn)行了擴(kuò)展,現(xiàn)在它已經(jīng)不僅僅是一個包含不同數(shù)據(jù)類型的數(shù)據(jù)結(jié)構(gòu)了,它包括了更多的功能。

          Struct能包含成員函數(shù)嗎?

          是的,答案是肯定的。現(xiàn)在就讓我寫一段代碼驗(yàn)證一下:

          復(fù)制代碼 代碼如下:

          /*

          ** 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;

          }

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

          Struct有自己的構(gòu)造函數(shù)嗎?

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

          復(fù)制代碼 代碼如下:

          /*

          ** 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可以有析構(gòu)函數(shù)么?

          讓我來驗(yàn)證一下:

          復(fù)制代碼 代碼如下:

          /*

          ** 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;

          }

          是的,完全支持析構(gòu)函數(shù)。

          Struct支持繼承么?

          再讓我寫代碼驗(yàn)證一下:

          復(fù)制代碼 代碼如下:

          /*

          ** 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;

          }

          運(yùn)行上述代碼,struct支持繼承。

          Struct支持多態(tài)么?

          寫代碼測試一下便知:

          復(fù)制代碼 代碼如下:

          /*

          ** 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關(guān)鍵字么?

          復(fù)制代碼 代碼如下:

          /*

          ** 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 主站蜘蛛池模板: 久久精品亚洲日本波多野结衣| 九色国产精品一区二区久久| 国产福利微视频一区二区| 国产成人久久精品激情| 午夜精品无人区乱码1区2区| 天堂久久天堂av色综合| 麻豆亚洲精品一区二区| 综1合AV在线播放| 色综合天天综合| 亚洲精品综合一区二区| 国产黄色带三级在线观看| 少妇xxxxx性开放| a毛片免费在线观看| 日韩人妻无码一区二区三区综合部 | 久久天堂av综合色无码专区| 九九re线精品视频在线观看视频| 一区二区三区四区亚洲自拍| 亚洲中文字幕日产无码成人片| 无码专区视频精品老司机| 好深好爽办公室做视频| 久久96热在精品国产高清| 亚洲精品中文字幕在线观| 免费AV手机在线观看片| 两个人看的www高清免费中文 | 一区二区三区一级黄色片| 久久综合久久美利坚合众国| 成av免费大片黄在线观看| 亚洲成人av免费一区| 国产乱色熟女一二三四区| 熟女系列丰满熟妇AV| av新版天堂在线观看| 青草午夜精品视频在线观看| 老鸭窝在线视频| 亚洲中文久久久精品无码| 国产精品免费看久久久 | 久久99九九精品久久久久蜜桃 | 91中文字幕一区在线| 亚洲一区二区三区国产精品| 亚洲一区二区三区无码久久| 国产午夜福利在线观看播放| 国产一区二区不卡在线|