C++中關(guān)鍵字Struct和Class的區(qū)別
Struct和Class的區(qū)別
今天這篇博文主要講解在C++中關(guān)鍵字struct和class的區(qū)別。這篇博文,將會系統(tǒng)的將這兩個關(guān)鍵字的不同面進行詳細的講解。
從語法上來講,class和struct做類型定義時只有兩點區(qū)別:
1.默認繼承權(quán)限,如果不指定,來自class的繼承按照private繼承處理,來自struct的繼承按照public繼承處理;
2.成員的默認訪問權(quán)限。class的成員默認是private權(quán)限,struct默認是public權(quán)限。以上兩點也是struct和class最基本的差別,也是最本質(zhì)的差別;
但是在C++中,struct進行了擴展,現(xiàn)在它已經(jīng)不僅僅是一個包含不同數(shù)據(jù)類型的數(shù)據(jù)結(jié)構(gòu)了,它包括了更多的功能。
Struct能包含成員函數(shù)嗎?
是的,答案是肯定的。現(xiàn)在就讓我寫一段代碼驗證一下:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.dbjr.com.cn
*/
#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;
}
以上的代碼會很正確的運行,是的;沒錯,struct能包含成員函數(shù)的。
Struct有自己的構(gòu)造函數(shù)嗎?
是的,可以的??匆韵聹y試代碼:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.dbjr.com.cn
*/
#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ù)么?
讓我來驗證一下:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.dbjr.com.cn
*/
#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支持繼承么?
再讓我寫代碼驗證一下:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.dbjr.com.cn
*/
#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;
}
運行上述代碼,struct支持繼承。
Struct支持多態(tài)么?
寫代碼測試一下便知:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.dbjr.com.cn
*/
#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)鍵字么?
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to http://www.dbjr.com.cn
*/
#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;
}
寫了這么多了,那么會出現(xiàn)這種一個狀況,如果是class的父類是struct關(guān)鍵字描述的,那么默認訪問屬性是什么?
當(dāng)出現(xiàn)這種情況時,到底默認是public繼承還是private繼承,取決于子類而不是基類。class可以繼承自struct修飾的類;同時,struct也可以繼承自class修飾的類,繼承屬性如下列描述:
class B:A{}; // private 繼承
class A{};
struct B:A{}; // public 繼承
最后,那么到底是使用struct,還是使用class呢?這個看個人喜好,但是這里有一個編程規(guī)范的問題,當(dāng)你覺得你要做的更像是一種數(shù)據(jù)結(jié)構(gòu)的話,那么用struct,如果你要做的更像是一種對象的話,那么用class。
- C++中的struct和class的區(qū)別詳解
- C++深入探索類和對象之封裝及class與struct的區(qū)別
- C++詳細講解函數(shù)調(diào)用與Struct和CLass的區(qū)別
- C++深入探索類真正的形態(tài)之struct與class
- C++基礎(chǔ) class、struct、union詳細
- C++ class和struct到底有什么區(qū)別詳解
- C++結(jié)構(gòu)體struct和類class區(qū)別詳解
- C++中聲明類的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字詳解
- 深入C++中struct與class的區(qū)別分析
- c++中struct和class的區(qū)別小結(jié)
相關(guān)文章
C++中String類的常用接口函數(shù)總結(jié)
這篇文章主要介紹了C++中Stirng類的常用接口函數(shù),文中有詳細的代碼示例供大家參考,對我們學(xué)習(xí)C++有一定的幫助,感興趣的同學(xué)可以跟著小編一起來學(xué)習(xí)2023-06-06C++中常見容器類的使用方法詳解(vector/deque/map/set)
C++中常見的容器類有vector、list、deque、map、set、unordered_map和unordered_set。下面將舉例直接說明各個容器的使用方法,希望對大家有所幫助2023-03-03C語言實現(xiàn)五子棋對戰(zhàn)系統(tǒng)
這篇文章主要為大家詳細介紹了C語言實現(xiàn)五子棋對戰(zhàn)系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05QT實現(xiàn)QML側(cè)邊導(dǎo)航欄的最簡方法
本文主要介紹了QT實現(xiàn)QML側(cè)邊導(dǎo)航欄的最簡方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06一文詳解matlab實現(xiàn)形態(tài)學(xué)圖像處理
這篇文章主要為大家介紹了matlab實現(xiàn)形態(tài)學(xué)圖像處理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03win10系統(tǒng)VS2019配置點云庫PCL1.12.1的詳細流程
這篇文章主要介紹了win10系統(tǒng)VS2019配置點云庫PCL1.12.1的教程與經(jīng)驗總結(jié),本文記錄小白在配置過程中踩過的一些小坑,需要的朋友可以參考下2022-07-07