C++中關(guān)鍵字Struct和Class的區(qū)別
Struct和Class的區(qū)別
今天這篇博文主要講解在C++中關(guān)鍵字struct和class的區(qū)別。這篇博文,將會(huì)系統(tǒng)的將這兩個(gè)關(guān)鍵字的不同面進(jìn)行詳細(xì)的講解。
從語(yǔ)法上來講,class和struct做類型定義時(shí)只有兩點(diǎn)區(qū)別:
1.默認(rèn)繼承權(quán)限,如果不指定,來自class的繼承按照private繼承處理,來自struct的繼承按照public繼承處理;
2.成員的默認(rèn)訪問權(quán)限。class的成員默認(rèn)是private權(quán)限,struct默認(rèn)是public權(quán)限。以上兩點(diǎn)也是struct和class最基本的差別,也是最本質(zhì)的差別;
但是在C++中,struct進(jìn)行了擴(kuò)展,現(xiàn)在它已經(jīng)不僅僅是一個(gè)包含不同數(shù)據(jù)類型的數(shù)據(jù)結(jié)構(gòu)了,它包括了更多的功能。
Struct能包含成員函數(shù)嗎?
是的,答案是肯定的?,F(xiàn)在就讓我寫一段代碼驗(yàn)證一下:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#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;
}
以上的代碼會(huì)很正確的運(yùn)行,是的;沒錯(cuò),struct能包含成員函數(shù)的。
Struct有自己的構(gòu)造函數(shù)嗎?
是的,可以的??匆韵聹y(cè)試代碼:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#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ù)么?
讓我來驗(yàn)證一下:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#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支持繼承么?
再讓我寫代碼驗(yàn)證一下:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#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;
}
運(yùn)行上述代碼,struct支持繼承。
Struct支持多態(tài)么?
寫代碼測(cè)試一下便知:
/*
** FileName : StructAndClassDiffDemo
** Author : Jelly Young
** Date : 2013/12/7
** Description : More information, please go to //www.jb51.net
*/
#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 //www.jb51.net
*/
#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;
}
寫了這么多了,那么會(huì)出現(xiàn)這種一個(gè)狀況,如果是class的父類是struct關(guān)鍵字描述的,那么默認(rèn)訪問屬性是什么?
當(dāng)出現(xiàn)這種情況時(shí),到底默認(rèn)是public繼承還是private繼承,取決于子類而不是基類。class可以繼承自struct修飾的類;同時(shí),struct也可以繼承自class修飾的類,繼承屬性如下列描述:
class B:A{}; // private 繼承
class A{};
struct B:A{}; // public 繼承
最后,那么到底是使用struct,還是使用class呢?這個(gè)看個(gè)人喜好,但是這里有一個(gè)編程規(guī)范的問題,當(dāng)你覺得你要做的更像是一種數(shù)據(jù)結(jié)構(gòu)的話,那么用struct,如果你要做的更像是一種對(duì)象的話,那么用class。
欄 目:C語(yǔ)言
下一篇:C語(yǔ)言求連續(xù)最大子數(shù)組和的方法
本文標(biāo)題:C++中關(guān)鍵字Struct和Class的區(qū)別
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3331.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
- 04-02c語(yǔ)言沒有round函數(shù) round c語(yǔ)言
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置