《c中class的用法.docx》由会员分享,可在线阅读,更多相关《c中class的用法.docx(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、c中class的用法c中class的用法c中class的用法的用法你知道吗?下面我就跟你们具体介绍下c中class的用法的用法,希望对你们有用。c中class的用法的用法如下:Struct和Class的区别今天这篇博文主要讲解在C+中关键字struct和class的区别。这篇博文,将会系统的将这两个关键字的不同面进行具体的讲解。从语法上来讲,class和struct做类型定义时只要两点区别:1.默认继承权限,假如不指定,来自class的继承根据private继承处理,来自struct的继承根据public继承处理;2.成员的默认访问权限。class的成员默认是private权限,struct默
2、认是public权限。以上两点也是struct和class最基本的差异,也是最本质的差异;但是在C+中,struct进行了扩展,如今它已经不仅仅是一个包含不同数据类型的数据构造了,它包括了更多的功能。Struct能包含成员函数吗?是的,答案是肯定的。如今就让我写一段代码验证一下:复制代码代码如下:/*FileName:StructAndClassDiffDemo*Author:JellyYoung*Date:2021/12/7*Description:Moreinformation,*/#includeiostreamusingnamespacestd;structTestinta;intge
3、tA()returna;voidsetA(inttemp)a=temp;intmain(intargc,char*argv)TesttestStruct;testStruct.setA(10);coutGetthevaluefromstruct:testStruct.getA()endl;Test*testStructPointer=newTest;testStructPointer-setA(20);coutGetthevaluefromstructagain:testStructPointer-getA()endl;deletetestStructPointer;return0;以上的代码
4、会很正确的运行,是的;没错,struct能包含成员函数的。Struct有本人的构造函数吗?是的,能够的。看下面测试代码:复制代码代码如下:/*FileName:StructAndClassDiffDemo*Author:JellyYoung*Date:2021/12/7*Description:Moreinformation,*/#includeiostreamusingnamespacestd;structTestinta;Test()a=100;intgetA()returna;voidsetA(inttemp)a=temp;intmain(intargc,char*argv)Testte
5、stStruct;testStruct.setA(10);coutGetthevaluefromstruct:testStruct.getA()endl;Test*testStructPointer=newTest;testStructPointer-setA(20);coutGetthevaluefromstructagain:testStruct.getA()endl;deletetestStructPointer;/testtheconstructorTesttestConstructor;coutSetthevaluebytheconstructandgetit:testConstru
6、ctor.getA()endl;return0;Struct能够有析构函数么?让我来验证一下:复制代码代码如下:/*FileName:StructAndClassDiffDemo*Author:JellyYoung*Date:2021/12/7*Description:Moreinformation,*/#includeiostreamusingnamespacestd;structTestinta;Test()a=100;intgetA()returna;voidsetA(inttemp)a=temp;Test()coutDestructorfunctioncalled.endl;intma
7、in(intargc,char*argv)TesttestStruct;testStruct.setA(10);coutGetthevaluefromstruct:testStruct.getA()endl;Test*testStructPointer=newTest;testStructPointer-setA(20);coutGetthevaluefromstructagain:testStruct.getA()endl;deletetestStructPointer;/testtheconstructorTesttestConstructor;coutSetthevaluebytheco
8、nstructandgetit:testConstructor.getA()endl;return0;是的,完全支持析构函数。Struct支持继承么?再让我写代码验证一下:复制代码代码如下:/*FileName:StructAndClassDiffDemo*Author:JellyYoung*Date:2021/12/7*Description:Moreinformation,*/#includeiostreamusingnamespacestd;structAinta;A()a=10;voidprint()coutIamfromAendl;structB:Aintb;B()a=30;/set
9、ato30b=20;/*voidprint()coutIamfromBendl;*/;intmain(intargc,char*argv)Bb1;coutb1.aendl;coutb1.bendl;b1.print();Aa1;couta1.aendl;a1.print();return0;运行上述代码,struct支持继承。Struct支持多态么?写代码测试一下便知:复制代码代码如下:/*FileName:StructAndClassDiffDemo*Author:JellyYoung*Date:2021/12/7*Description:Moreinformation,*/#include
10、iostreamusingnamespacestd;structAvirtualvoidprint()=0;structB:Avoidprint()coutIamfromBendl;structC:Avoidprint()coutIamfromCendl;intmain(intargc,char*argv)A*a1;B*b1=newB;C*c1=newC;a1=b1;a1-print();/callB,notAa1=c1;a1-print();/callC,notAreturn0;Struct支持Private、Protected和Public关键字么?复制代码代码如下:/*FileName:
11、StructAndClassDiffDemo*Author:JellyYoung*Date:2021/12/7*Description:Moreinformation,*/#includeiostreamusingnamespacestd;structAprivate:intb;protected:intc;public:A()b=10;c=20;d=30;intd;structB:AvoidprintA_C()coutA:cendl;/privatemembercannotsee/*voidprintA_B()coutA:bendl;*/voidprintA_D()coutA:dendl;intmain(intargc,char*argv)Aa1;Bb1;/privatemembercannotsee/couta1.bendl;/protectedmembercannotsee/couta1.cendl;/publicmembercanseecouta1.dendl;return0;c中class的用法c中class的用法的用法你知道吗?下面我就跟你们具体介绍下c中class的用法的用法,希望对你们有用。c中class的用法的用法如下:Struct和Class的区别今天这篇博文主要讲解在C+中关键字struct和class的区别。这篇博文,将会系统的将推荐度:
限制150内