从C到C++——结构体(struct)的涅槃

来源:互联网 发布:内涵段子网站源码 编辑:程序博客网 时间:2024/05/25 23:58

从C到C++,结构体(struct)涅槃重生,那么,C语言的结构体和C++结构体有什么不同呢,下面是一个测试程序,程序中大量的注释就算作本文的讲解吧!

 

/*- ==========================================================*     文件名  :TestCppStruct.cpp*     开发人员:袁培荣*     当前版本:1.0.0.2595*     创建时间:2012-05-17*     修改时间:2012-05-17*     功能说明:C++结构体功能测试*     版权说明:版权所有 袁培荣 YuanPeirong *     编译环境:Windows 7(x64) SP1 简体中文专业版*     编译器:  Visual Studio 2010 SP1(中文旗舰版)                MinGW 20111108                Visual C++ 6.0 SP6(中文企业版)- ==========================================================*/#include <iostream>using std::cout;using std::endl;//====================================================================struct STest{    int a;    char c;    STest()    {        cout<<"STest结构体构造函数被执行"<<endl;        a=10;        c='S';    };};//====================================================================struct STest2{    int a;    char c;    STest2();};STest2::STest2(){    cout<<"STest2结构体构造函数被执行"<<endl;    a=15;    c='T';}//====================================================================struct STest3{    int a;    char c;    void Test()    {        cout<<"STest3结构体Test函数被执行"<<endl;        a=10;        c='S';    };};//====================================================================struct STest4{    int a;    char c;    void Test();};void STest4::Test(){    cout<<"STest4结构体Test函数被执行"<<endl;    a=15;    c='T';}//====================================================================struct STest5{    int a;    void Test();private:    char c;    void Test2();    };void STest5::Test(){    cout<<"STest5结构体Test函数被执行"<<endl;    a=16;    c='X';}void STest5::Test2(){    cout<<"STest5结构体Test2函数被执行"<<endl;    a=17;    c='Y';}//====================================================================int main(int argc, char* argv[]){    //====================================================================    STest s11;  //在C语言中应为 struct STest s11;  在C++中struct可省略    STest2 s21; //    cout<<"s11: "<<s11.a<<" "<<s11.c<<endl;    cout<<"s21: "<<s21.a<<" "<<s21.c<<endl;    s11.a=100;    s21.c='Z';    cout<<"直接修改了s11和s21的值!"<<endl;    cout<<"s11: "<<s11.a<<" "<<s11.c<<endl;    cout<<"s21: "<<s21.a<<" "<<s21.c<<endl;    //====================================================================    // C++的结构体有构造函数,C语言中结构体不能有函数,更别谈构造函数    //====================================================================        //====================================================================    // STest s12={1,'A'};    // STest s22={2,'B'};    // cout<<"s12: "<<s12.a<<" "<<s12.c<<endl;    // cout<<"s22: "<<s22.a<<" "<<s22.c<<endl;    //====================================================================    // error: `s12' must be initialized by constructor, not by `{...}'    // error: `s22' must be initialized by constructor, not by `{...}'    // 结构体一旦有构造函数就不能通过 {...} 方式初始化    //====================================================================        //====================================================================    STest3 s31={1,'A'};    STest4 s41={2,'B'};    cout<<"通过 {...} 方式初始化 了s31和s41的值"<<endl;    cout<<"s31: "<<s31.a<<" "<<s31.c<<endl;    cout<<"s41: "<<s41.a<<" "<<s41.c<<endl;    s31.Test();    s41.Test();    cout<<"s31: "<<s31.a<<" "<<s31.c<<endl;    cout<<"s41: "<<s41.a<<" "<<s41.c<<endl;    s31.a=100;    s41.c='Z';    cout<<"直接修改了s31和s41的值!"<<endl;    cout<<"s31: "<<s31.a<<" "<<s31.c<<endl;    cout<<"s41: "<<s41.a<<" "<<s41.c<<endl;    //====================================================================    // C++的结构体可以有成员函数    // C++的结构体没有构造函数时可以通过 {...} 方式初始化    //====================================================================            //====================================================================    //STest5 s51={5,'A'};      // error: `s51' must be initialized by constructor, not by `{...}'        //s51.Test2();    //error: `void STest5::Test2()' is private        //s51.c='Z';    // error: `char STest5::c' is private        STest5 s52;    s52.a=52;    cout<<"s52.a="<<s52.a<<endl;    s52.Test();    cout<<"s52.a="<<s52.a<<endl;    s52.a=10;    cout<<"s52.a="<<s52.a<<endl;    //====================================================================    // 结构体STest5的测试说明:    // C++中可以为结构体的成员设定访问控制级别 private public protected    // C++中一旦为结构体设定访问控制级别,那么这个结构体其实就是类    // 所以不可以通过 {...} 方式初始化    // 类中的访问控制级别和结构体是一致的,但是缺省时:    // 类中是 private 而结构体中是 public    // 这是唯一的区别    //====================================================================        return 0; //在C语言和早期C++中此句不可以省略,在标准C++中此句可省略 }//====================================================================//运行结果//====================================================================// STest结构体构造函数被执行// STest2结构体构造函数被执行// s11: 10 S// s21: 15 T// 直接修改了s11和s21的值!// s11: 100 S// s21: 15 Z// 通过 {...} 方式初始化 了s31和s41的值// s31: 1 A// s41: 2 B// STest3结构体Test函数被执行// STest4结构体Test函数被执行// s31: 10 S// s41: 15 T// 直接修改了s31和s41的值!// s31: 100 S// s41: 15 Z// s52.a=52// STest5结构体Test函数被执行// s52.a=16// s52.a=10//====================================================================// 总结: C++的结构体可以分两部分来看// 1.保持和C语言兼容的结构体,不涉及函数// 2.C++支持的结构体,这种结构类事实上和类class功能相同,但有一点区别// 区别在于// 类中默认的访问控制级别为     private// 结构体中默认的访问控制级别为 public// 是的,我们可以在编程时用 private public protected来修饰结构体中的成员// C++的结构体就是C语言的结构体和C++中的类的综合体//====================================================================//====================================================================// 以上测试的错误信息是在MinGW编译器上编译时复制的// 以上代码在                // Visual Studio 2010 SP1(中文旗舰版)                // MinGW 20111108                // Visual C++ 6.0 SP6(中文企业版)// 这三个编译器上编译通过//====================================================================

原创粉丝点击