欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析c與c++中struct的區(qū)別

 更新時間:2013年07月25日 09:03:57   作者:  
c與c++中struct的區(qū)別你是否了解,下面小編就詳細(xì)的為大家介紹一下

這里有兩種情況下的區(qū)別。
(1)C的struct與C++的class的區(qū)別。
(2)C++中的struct和class的區(qū)別。
在第一種情況下,struct與class有著非常明顯的區(qū)別。C是一種過程化的語言,struct只是作為一種復(fù)雜數(shù)據(jù)類型定義,struct中只能定義成員變量,不能定義成員函數(shù)(在純粹的C語言中,struct不能定義成員函數(shù),只能定義變量)。例如下面的C代碼片斷:

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

        struct Point
        {
                int x; // 合法
                int y; // 合法
                void print()
                {
                        printf("Point print\n"); //編譯錯誤
                };
}9        ;


這里第7行會出現(xiàn)編譯錯誤,提示如下的錯誤消息:“函數(shù)不能作為Point結(jié)構(gòu)體的成員”。因此大家看到在第一種情況下struct只是一種數(shù)據(jù)類型,不能使用面向?qū)ο缶幊獭?/P>

現(xiàn)在來看第二種情況。首先請看下面的代碼:

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

       #include <iostream>
        using namespace std;
        class CPoint
        {
                int x;                  //默認(rèn)為private
                int y;                  //默認(rèn)為private
                void print()             //默認(rèn)為private
                {
                        cout << "CPoint: (" << x << ", " << y << ")" << endl;
                }
        public:
                CPoint(int x, int y)      //構(gòu)造函數(shù),指定為public
                {
                        this->x = x;
                        this->y = y;
                }
                void print1() //public
                {
                        cout << "CPoint: (" << x << ", " << y << ")" << endl;
                }
        };

        struct SPoint
        {
                int x;              //默認(rèn)為public
                int y;              //默認(rèn)為public
                void print()         //默認(rèn)為public
                {
                        cout << "SPoint: (" << x << ", " << y << ")" << endl;
                }
                SPoint(int x, int y)  //構(gòu)造函數(shù),默認(rèn)為public
                {
                        this->x = x;
                        this->y = y;
                }
        private:
                void print1()      //private類型的成員函數(shù)
                {
                        cout << "SPoint: (" << x << ", " << y << ")" << endl;
                }
        };

        int main(void)
        {
                CPoint cpt(1, 2);  //調(diào)用CPoint帶參數(shù)的構(gòu)造函數(shù)
                SPoint spt(3, 4);  //調(diào)用SPoint帶參數(shù)的構(gòu)造函數(shù)

                cout << cpt.x << " " << cpt.y << endl;  //編譯錯誤
                cpt.print();       //編譯錯誤
                cpt.print1();      //合法

                spt.print();      //合法
                spt.print1();     //編譯錯誤
                cout << spt.x << " " << spt.y << endl;  //合法

                return 0;
        }


在上面的程序里,struct還有構(gòu)造函數(shù)和成員函數(shù),其實(shí)它還擁有class的其他特性,例如繼承、虛函數(shù)等。因此C++中的struct擴(kuò)充了C的struct功能。那它們有什么不同呢?

main函數(shù)內(nèi)的編譯錯誤全部是因為訪問private成員而產(chǎn)生的。因此我們可以看到class中默認(rèn)的成員訪問權(quán)限是private的,而struct中則是public的。在類的繼承方式上,struct和class又有什么區(qū)別?請看下面的程序:

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

       #include <iostream>
        using namespace std;
        class CBase
        {
        public:
                void print()                //public成員函數(shù)
                {
                        cout << "CBase: print()..." << endl;
                }
        };
        class CDerived1 : CBase        //默認(rèn)private繼承
        {
        };

        class CDerived2 : public Cbase   //指定public繼承
        {
        };

        struct SDerived1 : Cbase        //默認(rèn)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繼承默認(rèn)是private繼承,而struct繼承默認(rèn)是public繼承。另外,在C++模板中,類型參數(shù)前面可以使用class或typename,如果使用struct,則含義不同,struct后面跟的是“non-type template parameter”,而class或typename后面跟的是類型參數(shù)。
事實(shí)上,C++中保留struct的關(guān)鍵字是為了使C++編譯器能夠兼容C開發(fā)的程序。

答案:
分以下所示兩種情況。
C的struct與C++的class的區(qū)別:struct只是作為一種復(fù)雜數(shù)據(jù)類型定義,不能用于面向?qū)ο缶幊獭?BR>C++中的struct和class的區(qū)別:對于成員訪問權(quán)限以及繼承方式,class中默認(rèn)的是private的,而struct中則是public的。class還可以用于表示模板類型,struct則不行。

相關(guān)文章

最新評論