C++中关键字Struct和Class的区别
Struct和Class的区别
今天这篇博文主要讲解在C++中关键字struct和class的区别。这篇博文,将会系统的将这两个关键字的不同面进行详细的讲解。
从语法上来讲,class和struct做类型定义时只有两点区别:
1.默认继承权限,如果不指定,来自class的继承按照private继承处理,来自struct的继承按照public继承处理;
2.成员的默认访问权限。class的成员默认是private权限,struct默认是public权限。以上两点也是struct和class最基本的差别,也是最本质的差别;
但是在C++中,struct进行了扩展,现在它已经不仅仅是一个包含不同数据类型的数据结构了,它包括了更多的功能。
Struct能包含成员函数吗?
是的,答案是肯定的。现在就让我写一段代码验证一下:
/* **FileName :StructAndClassDiffDemo **Author :JellyYoung **Date :2013/12/7 **Description :Moreinformation,pleasegotohttps://www.nhooo.com */ #include<iostream> usingnamespacestd; structTest { inta; intgetA() { returna; } voidsetA(inttemp) { a=temp; } }; intmain(intargc,char*argv[]) { TesttestStruct; testStruct.setA(10); cout<<"Getthevaluefromstruct:"<<testStruct.getA()<<endl; Test*testStructPointer=newTest; testStructPointer->setA(20); cout<<"Getthevaluefromstructagain:"<<testStructPointer->getA()<<endl; deletetestStructPointer; return0; }
以上的代码会很正确的运行,是的;没错,struct能包含成员函数的。
Struct有自己的构造函数吗?
是的,可以的。看以下测试代码:
/* **FileName :StructAndClassDiffDemo **Author :JellyYoung **Date :2013/12/7 **Description :Moreinformation,pleasegotohttps://www.nhooo.com */ #include<iostream> usingnamespacestd; structTest { inta; Test() { a=100; } intgetA() { returna; } voidsetA(inttemp) { a=temp; } }; intmain(intargc,char*argv[]) { TesttestStruct; testStruct.setA(10); cout<<"Getthevaluefromstruct:"<<testStruct.getA()<<endl; Test*testStructPointer=newTest; testStructPointer->setA(20); cout<<"Getthevaluefromstructagain:"<<testStruct.getA()<<endl; deletetestStructPointer; //testtheconstructor TesttestConstructor; cout<<"Setthevaluebytheconstructandgetit:"<<testConstructor.getA()<<endl; return0; }
Struct可以有析构函数么?
让我来验证一下:
/* **FileName :StructAndClassDiffDemo **Author :JellyYoung **Date :2013/12/7 **Description :Moreinformation,pleasegotohttps://www.nhooo.com */ #include<iostream> usingnamespacestd; structTest { inta; Test() { a=100; } intgetA() { returna; } voidsetA(inttemp) { a=temp; } ~Test() { cout<<"Destructorfunctioncalled."<<endl; } }; intmain(intargc,char*argv[]) { TesttestStruct; testStruct.setA(10); cout<<"Getthevaluefromstruct:"<<testStruct.getA()<<endl; Test*testStructPointer=newTest; testStructPointer->setA(20); cout<<"Getthevaluefromstructagain:"<<testStruct.getA()<<endl; deletetestStructPointer; //testtheconstructor TesttestConstructor; cout<<"Setthevaluebytheconstructandgetit:"<<testConstructor.getA()<<endl; return0; }
是的,完全支持析构函数。
Struct支持继承么?
再让我写代码验证一下:
/* **FileName :StructAndClassDiffDemo **Author :JellyYoung **Date :2013/12/7 **Description :Moreinformation,pleasegotohttps://www.nhooo.com */ #include<iostream> usingnamespacestd; structA { inta; A() { a=10; } voidprint() { cout<<"IamfromA"<<endl; } }; structB:A { intb; B() { a=30;//setato30 b=20; } /*voidprint() { cout<<"IamfromB"<<endl; }*/ }; intmain(intargc,char*argv[]) { Bb1; cout<<b1.a<<endl; cout<<b1.b<<endl; b1.print(); Aa1; cout<<a1.a<<endl; a1.print(); return0; }
运行上述代码,struct支持继承。
Struct支持多态么?
写代码测试一下便知:
/* **FileName :StructAndClassDiffDemo **Author :JellyYoung **Date :2013/12/7 **Description :Moreinformation,pleasegotohttps://www.nhooo.com */ #include<iostream> usingnamespacestd; structA { virtualvoidprint()=0; }; structB:A { voidprint() { cout<<"IamfromB"<<endl; } }; structC:A { voidprint() { cout<<"IamfromC"<<endl; } }; intmain(intargc,char*argv[]) { A*a1; B*b1=newB; C*c1=newC; a1=b1; a1->print();//callB,notA a1=c1; a1->print();//callC,notA return0; }
Struct支持Private、Protected和Public关键字么?
/* **FileName :StructAndClassDiffDemo **Author :JellyYoung **Date :2013/12/7 **Description :Moreinformation,pleasegotohttps://www.nhooo.com */ #include<iostream> usingnamespacestd; structA { private: intb; protected: intc; public: A() { b=10; c=20; d=30; } intd; }; structB:A { voidprintA_C() { cout<<A::c<<endl; }; //privatemembercannotsee /*voidprintA_B() { cout<<A::b<<endl; }*/ voidprintA_D() { cout<<A::d<<endl; } }; intmain(intargc,char*argv[]) { Aa1; Bb1; //privatemembercannotsee //cout<<a1.b<<endl; //protectedmembercannotsee //cout<<a1.c<<endl; //publicmembercansee cout<<a1.d<<endl; return0; }
写了这么多了,那么会出现这种一个状况,如果是class的父类是struct关键字描述的,那么默认访问属性是什么?
当出现这种情况时,到底默认是public继承还是private继承,取决于子类而不是基类。class可以继承自struct修饰的类;同时,struct也可以继承自class修饰的类,继承属性如下列描述:
classB:A{};//private继承 classA{}; structB:A{};//public继承
最后,那么到底是使用struct,还是使用class呢?这个看个人喜好,但是这里有一个编程规范的问题,当你觉得你要做的更像是一种数据结构的话,那么用struct,如果你要做的更像是一种对象的话,那么用class。