class和struct有什麼區別?

來源:互聯網
上載者:User

標籤:c++


這裡有兩種情況下的區別。
(1)C的struct與C++的class的區別。
(2)C++中的struct和class的區別。
在第一種情況下,struct與class有著非常明顯的區別。C是一種過程化的語言,struct只是作為一種複雜資料類型定義,struct中只能定義成員變數,不能定義成員函數。例如下面的C代碼片斷:
        struct Point
        {
                int x; // 合法
                int y; // 合法
                void print()
                {
                        printf("Point print\n"); //編譯錯誤
                };
}9        ;
這裡第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又有什麼區別?請看下面的程式:
        #include <iostream>
        using namespace std;
        class CBase
        {
        public:
                void print()                //public成員函數
                {
                        cout << "CBase: print()..." << endl;
                }
        };
        class CDerived1 : CBase        //預設private繼承
        {
        };
        
        class CDerived2 : public Cbase   //指定public繼承
        {
        };
        
        struct SDerived1 : Cbase        //預設public繼承
        {
        };
        
        struct SDerived2 : private Cbase  //指定public繼承
        {
        };
        
        int main()
        {
                CDerived1 cd1;
                CDerived2 cd2;
                SDerived1 sd1;
                SDerived2 sd2;
        
                cd1.print();    //編譯錯誤
                cd2.print();
                sd1.print();
                sd2.print();    //編譯錯誤
        
                return 0;
        }
可以看到,以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則不行。

class和struct有什麼區別?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.