c与c++中struct区别
生活随笔
收集整理的這篇文章主要介紹了
c与c++中struct区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里有兩種情況下的區別。
(1)C的struct與C++的class的區別。
(2)C++中的struct和class的區別。
? ? ? 在第一種情況下,struct與class有著非常明顯的區別。C是一種過程化的語言,struct只是作為一種復雜數據類型定義,struct中只能定義成員變量,不能定義成員函數(在純粹的C語言中,struct不能定義成員函數,只能定義變量)。例如下面的C代碼片斷:
? ?? ???struct Point
? ?? ???{
? ?? ?? ?? ?? ? int x; // 合法
? ?? ?? ?? ?? ? int y; // 合法
? ?? ?? ?? ?? ??void print()
? ?? ?? ?? ?? ? {
? ?? ?? ?? ?? ?? ?? ?? ?printf("Point print\n"); //編譯錯誤
? ?? ?? ?? ?? ? };
}9? ?? ???;
? ?? ???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只是作為一種復雜數據類型定義,不能用于面向對象編程。
(1)C的struct與C++的class的區別。
(2)C++中的struct和class的區別。
? ? ? 在第一種情況下,struct與class有著非常明顯的區別。C是一種過程化的語言,struct只是作為一種復雜數據類型定義,struct中只能定義成員變量,不能定義成員函數(在純粹的C語言中,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則不行。
總結
以上是生活随笔為你收集整理的c与c++中struct区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS2010下安装boost库
- 下一篇: Windows下创建静态库