最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中關(guān)鍵字Struct和Class的區(qū)別

 更新時(shí)間:2014年09月30日 16:03:15   作者:果凍想  
這篇文章主要介紹了C++中關(guān)鍵字Struct和Class的區(qū)別,本文用大量實(shí)例講解了Struct和Class的區(qū)別,需要的朋友可以參考下

Struct和Class的區(qū)別

今天這篇博文主要講解在C++中關(guān)鍵字struct和class的區(qū)別。這篇博文,將會(huì)系統(tǒng)的將這兩個(gè)關(guān)鍵字的不同面進(jìn)行詳細(xì)的講解。

從語(yǔ)法上來(lái)講,class和struct做類(lèi)型定義時(shí)只有兩點(diǎn)區(qū)別:

1.默認(rèn)繼承權(quán)限,如果不指定,來(lái)自class的繼承按照private繼承處理,來(lái)自struct的繼承按照public繼承處理;

2.成員的默認(rèn)訪問(wè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ù)類(lèi)型的數(shù)據(jù)結(jié)構(gòu)了,它包括了更多的功能。

Struct能包含成員函數(shù)嗎?

是的,答案是肯定的。現(xiàn)在就讓我寫(xiě)一段代碼驗(yàn)證一下:

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

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to http://m.fzitv.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)行,是的;沒(méi)錯(cuò),struct能包含成員函數(shù)的。

Struct有自己的構(gòu)造函數(shù)嗎?

是的,可以的??匆韵聹y(cè)試代碼:

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

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to http://m.fzitv.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ù)么?

讓我來(lái)驗(yàn)證一下:

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

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to http://m.fzitv.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支持繼承么?

再讓我寫(xiě)代碼驗(yàn)證一下:

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

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to http://m.fzitv.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)么?

寫(xiě)代碼測(cè)試一下便知:

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

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to http://m.fzitv.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)鍵字么?

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

/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to http://m.fzitv.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&lt;&lt;endl;
 
    // public member can see
    cout<<a1.d<<endl;
 
    return 0;
}

寫(xiě)了這么多了,那么會(huì)出現(xiàn)這種一個(gè)狀況,如果是class的父類(lèi)是struct關(guān)鍵字描述的,那么默認(rèn)訪問(wèn)屬性是什么?

當(dāng)出現(xiàn)這種情況時(shí),到底默認(rèn)是public繼承還是private繼承,取決于子類(lèi)而不是基類(lèi)。class可以繼承自struct修飾的類(lèi);同時(shí),struct也可以繼承自class修飾的類(lèi),繼承屬性如下列描述:

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

class B:A{}; // private 繼承
 
class A{};
struct B:A{}; // public 繼承

最后,那么到底是使用struct,還是使用class呢?這個(gè)看個(gè)人喜好,但是這里有一個(gè)編程規(guī)范的問(wèn)題,當(dāng)你覺(jué)得你要做的更像是一種數(shù)據(jù)結(jié)構(gòu)的話,那么用struct,如果你要做的更像是一種對(duì)象的話,那么用class。

相關(guān)文章

  • 詳解C++ 拷貝構(gòu)造函數(shù)和賦值運(yùn)算符

    詳解C++ 拷貝構(gòu)造函數(shù)和賦值運(yùn)算符

    本文主要介紹了拷貝構(gòu)造函數(shù)和賦值運(yùn)算符的區(qū)別,以及在什么時(shí)候調(diào)用拷貝構(gòu)造函數(shù)、什么情況下調(diào)用賦值運(yùn)算符。最后,簡(jiǎn)單的分析了下深拷貝和淺拷貝的問(wèn)題。有需要的朋友可以看下
    2016-12-12
  • C++中的std::initializer_list使用解讀

    C++中的std::initializer_list使用解讀

    這篇文章主要介紹了C++中的std::initializer_list使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • C++通過(guò)自定義函數(shù)求一元二次方程的根

    C++通過(guò)自定義函數(shù)求一元二次方程的根

    這篇文章主要介紹了C++通過(guò)自定義函數(shù)求一元二次方程的根,涉及C++數(shù)學(xué)運(yùn)算相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2016-05-05
  • C++之文件輸入/輸出流類(lèi)解讀

    C++之文件輸入/輸出流類(lèi)解讀

    這篇文章主要介紹了C++之文件輸入/輸出流類(lèi),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • C++超詳細(xì)講解智能指針

    C++超詳細(xì)講解智能指針

    為了解決內(nèi)存泄漏的問(wèn)題,C++中提出了智能指針。內(nèi)存泄漏的產(chǎn)生原因有很多,即使我們正確的使用malloc和free關(guān)鍵字也有可能產(chǎn)生內(nèi)存泄漏,如在malloc和free之間如果存在拋異常,那也會(huì)產(chǎn)生內(nèi)存泄漏。這種問(wèn)題被稱(chēng)為異常安全
    2022-06-06
  • C++實(shí)現(xiàn)String與UF8互轉(zhuǎn)

    C++實(shí)現(xiàn)String與UF8互轉(zhuǎn)

    這篇文章介紹了C++實(shí)現(xiàn)String與UF8互轉(zhuǎn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C++中如何調(diào)用C語(yǔ)言的代碼實(shí)現(xiàn)

    C++中如何調(diào)用C語(yǔ)言的代碼實(shí)現(xiàn)

    這篇文章主要介紹了C++中如何調(diào)用C語(yǔ)言的代碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • c++中struct使用注意事項(xiàng)

    c++中struct使用注意事項(xiàng)

    本文通過(guò)2個(gè)小示例給大家展示了一下c++中struct使用的注意事項(xiàng),希望對(duì)大家學(xué)習(xí)C++能夠有所幫助。
    2016-01-01
  • C++?Date類(lèi)的具體使用(構(gòu)建,重載等)

    C++?Date類(lèi)的具體使用(構(gòu)建,重載等)

    本文主要介紹了C++?Date類(lèi)的具體使用(構(gòu)建,重載等),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C++ 中malloc()和free()函數(shù)的理解

    C++ 中malloc()和free()函數(shù)的理解

    這篇文章主要介紹了C++ 中malloc()和free()函數(shù)的理解的相關(guān)資料,這里提供用法示例幫助大家理解這部分知識(shí),需要的朋友可以參考下
    2017-08-08

最新評(píng)論

北京市| 南平市| 湘潭市| 聂拉木县| 从江县| 厦门市| 安徽省| 行唐县| 佛坪县| 西乌珠穆沁旗| 武胜县| 峡江县| 宣威市| 辽源市| 利川市| 福泉市| 花莲县| 长汀县| 孟州市| 蓝田县| 平乡县| 卢湾区| 靖西县| 庆元县| 丹东市| 辽中县| 海晏县| 道真| 禹城市| 泸州市| 长沙市| 克东县| 疏附县| 阳高县| 汉中市| 增城市| 牡丹江市| 东乡县| 娱乐| 乐业县| 临桂县|