struct與class的區別(分別在c和c++中)

來源:互聯網
上載者:User

標籤:方式   錯誤   參數   編譯錯誤   end   解析   color   構造   代碼   

註:以下內容未經博主同意,不得轉載。


解析:
這裡有兩種情況下的區別。
(1)C的struct與C++的class的區別。
(2)C++中的struct和class的區別。
在第一種情況下,struct與class有著非常明顯的區別。C是一種過程化的語言,struct只是作為一種複雜資料類型定義,struct中只能定義成員變數,不能定義成員函數。例如下面的C代碼片斷:

 1 struct Point 2 { 3    int x; // 合法 4    int y; // 合法 5    void print() 6     { 7       printf("Point print\n"); //編譯錯誤 8    }; 9 };10  

這裡第7行會出現編譯錯誤,提示如下的錯誤訊息:“函數不能作為Point結構體的成員”。因此大家看到在第一種情況下struct只是一種資料類型,不能使用物件導向編程。現在來看第二種情況。首先請看下面的代碼:

#include <iostream>using namespace std;class CPoint{      int x;                  //預設為private      int y;                  //預設為private      void print()            //預設為private      {          cout << "CPoint: (" << x << ", " << y << ")" << endl;      }    public:        CPoint(int x, int y)      //建構函式,指定為public          {          this->x = x;          this->y = y;          }       void print1() //public       {          cout << "CPoint: (" << x << ", " << y << ")" << endl;       }};struct SPoint{      int x;              //預設為public      int y;              //預設為public      void print()         //預設為public      {          cout << "SPoint: (" << x << ", " << y << ")" << endl;      }      SPoint(int x, int y)  //建構函式,預設為public      {          this->x = x;          this->y = y;      }    private:      void print1()      //private類型的成員函數      {         cout << "SPoint: (" << x << ", " << y << ")" << endl;      }};int main(void){   CPoint cpt(1, 2);  //調用CPoint帶參數的建構函式   SPoint spt(3, 4);  //調用SPoint帶參數的建構函式          cout << cpt.x << " " << cpt.y << endl;  //編譯錯誤   cpt.print();       //編譯錯誤   cpt.print1();      //合法   spt.print();      //合法   spt.print1();     //編譯錯誤   cout << spt.x << " " << spt.y << endl;  //合法   return 0;}

在上面的程式裡,struct還有建構函式和成員函數,其實它還擁有class的其他特性,例如繼承、虛函數等。因此C++中的struct擴充了C的struct功能。那它們有什麼不同呢?
main函數內的編譯錯誤全部是因為訪問private成員而產生的。因此我們可以看到class中預設的成員存取權限是private的,而struct中則是public的。在類的繼承方式上,struct和class又有什麼區別?請看下面的程式:

 1 #include <iostream> 2 using namespace std; 3 class CBase 4 { 5     public: 6       void print()                //public成員函數 7        { 8          cout << "CBase: print()..." << endl; 9         }10 };11 class CDerived1 : CBase        //預設private繼承12 {13 };14 15 class CDerived2 : public Cbase   //指定public繼承16 {17 };18 19 struct SDerived1 : Cbase        //預設public繼承20 {21 };22 23 struct SDerived2 : private Cbase  //指定public繼承24 {25 };26 27 int main()28 {29   CDerived1 cd1;30   CDerived2 cd2;31   SDerived1 sd1;32   SDerived2 sd2;33 34   cd1.print();    //編譯錯誤35   cd2.print();36   sd1.print();37   sd2.print();    //編譯錯誤38 39   return 0;40 }

可以看到,以private方式繼承父類的子類對象不能訪問父類的public成員。class繼承預設是private繼承,而struct繼承預設是public繼承。
另外,在C++模板中,型別參數前面可以使用class或typename,如果使用struct,則含義不同,struct後面跟的是“non-type template parameter”,而class或typename後面跟的是型別參數。
事實上,C++中保留struct的關鍵字是為了使C++編譯器能夠相容C開發的程式。


答案:
分以下所示兩種情況。
C的struct與C++的class的區別:struct只是作為一種複雜資料類型定義,不能用於物件導向編程。
C++中的struct和class的區別:對於成員存取權限以及繼承方式,class中預設的是private的,而struct中則是public的。class還可以用於表示模板類型,struct則不行。
 

struct與class的區別(分別在c和c++中)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.