Knowledge that you must know in C + +

Source: Internet
Author: User

//In-depth understanding of inheritance and the spatial size of virtual functions#include <iostream>using namespace STD;classa{Chara[ -];};classB: Publica{Charb[ -];};classC: Publica{Charc[ -];};classD: PublicB Publicc{Chard[ -];};//virtual inheritance for other classesclassE: Public Virtuala{Chare[ -];};classF: Public Virtualb{Charf[ -];};the method of getting the length of the array with//function parameterTemplate<TypeNameTintN>voidPrintarrlength (T (&AMP;C) [N]) {cout<<"c[] spare mem space is"<<sizeof(C) << Endl;cout<<"c[] length is"<< N << Endl;cout<<"The array typebyte is"<<sizeof(C)/n << Endl;}/ * Detailed explanation virtual inheritance: In the inheritance definition contains the Virtual keyword Inheritance Relationship virtual base class: In the virtual function system, the base class that inherits from virtual, need to notice is: class Csubclass:public virtual CBase { Where CBase is called the virtual base class of Csubclass, rather than saying that CBase is a virtual base class * /structcbaseclass1{CBaseClass1 (size_t N): M_val (n) {} Public: size_t M_val;};structCSubClassV1: Public Virtualcbaseclass1{CSubClassV1 (size_t N): CBaseClass1 (n) {}};structCSubClassV2: Public Virtualcbaseclass1{CSubClassV2 (size_t N): CBaseClass1 (n) {}};structCDIAMONDCLASS1: PublicCSubClassV1, Publiccsubclassv2{CDiamondClass1 (size_t N): CBaseClass1 (n), CSubClassV1 (n), CSubClassV2 (n) {}};structCDIAMONDSUBCLASS1: Publiccdiamondclass1{CDiamondSubClass1 (size_t N): CBaseClass1 (n), CDiamondClass1 (n) {}};//function pointervoidFA () {cout<<"Hello fa"<< Endl; }voidFB () {cout<<"Hello fb"<< Endl; }voidFC () {cout<<"Hello FC"<< Endl; }//define function pointer, return type void, parameter passed to voidtypedef void(*FUNCPTR) (void);//Generate two-dimensional arrays in the heap areavoidNew2darray (intXintY) {int* * p=New int*[y]; for(intI=0; i<y;i++) p[i]=New int[x];intCount=0; for(intI=0; i<x;i++) for(intj=0; j<y;j++) p[i][j]=count++; for(intI=0; i<x;i++) { for(intj=0; j<y;j++)cout<< P[i][j] <<" ";cout<< Endl; }}//test SectionintMain () {///Direct inheritance the size of the space occupied by each class    cout<<"sizeof (A)"<<sizeof(A) << Endl;//100    cout<<"sizeof (B)"<<sizeof(B) << Endl;//200    cout<<"sizeof (C)"<<sizeof(C) << Endl;//200    cout<<"sizeof (D)"<<sizeof(D) << Endl;//500    //For virtual inheritance of the size of the class function space, there are also virtual function pointers    cout<<"sizeof (E)"<<sizeof(E) << Endl;//200+4    cout<<"sizeof (F)"<<sizeof(F) << Endl;//300+4    //For examples of implicit conversions in multiple inheritanced* PD =NewD c* PC =dynamic_cast<C*> (PD); b* PB =dynamic_cast<B*> (PD);cout<< (int)(void*) PC << Endl;cout<< (int)(void*) PB << Endl;cout<< (int)(void*) PC-(int)(void*) PB << Endl;//200    //Test get array length    intp[]={1,2,3,4,5}; Printarrlength (P);//test part of the function pointerFuncptr FP;CharChoosefunc;Cin>> Choosefunc;Switch(Choosefunc) { Case ' A ': fp=&fa; Break; Case ' B ': fp=&fb; Break; Case ' C ': FP=FC; Break;default: Break; }//Don't forget to call here(*FP) ();//test the generation of two-dimensional arraysNew2darray (4,5);return 0;}

Knowledge that you must know in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.