Survival guide 7 CSE ?? Object Oriented Design Spring C Survival Guide Version Basic Notes on Syntax of pointers references classes strings streams and vectors Jim Fawcett January C Survival Guide CCSE ?? Object Oriented Design Spring C Pointers and Refer
CSE ?? Object Oriented Design Spring C Survival Guide Version Basic Notes on Syntax of pointers references classes strings streams and vectors Jim Fawcett January C Survival Guide CCSE ?? Object Oriented Design Spring C Pointers and References Create pointers and references Note in declaration is a reference in expression is an address for example on left of assignment is a reference on right of assignment is an address a int x declare and de ?ne x int pInt x create pointer to x b int y declare and de ?ne array of ints int pIntArray y point to beginning of array c struct CStructType int x double d char z CStruct - ? z ? declare a structure type and de ?ne one CStructType pStr CStruct create a pointer to that structure d int rX x create a reference to an integer e int fun const int x ? create a reference on the stack frame of fun and return a reference to something Use pointers and references a int z pInt b pInt - c pIntArray d pStr- d e int w rX f rX g int u fun x return the contents of the location pointed to change the value of the location pointed to same as y change the value of CStruct d return value of reference e g value of x modify value of reference e g value of x create a reference to x on the stack frame of fun If fun changes this value then the caller ? s value is also changed Assign the value of the returned integer to u Allocating and deallocating memory When new is invoked memory is allocated and then initialized with a class constructor to create a functioning object When delete is invoked the class destructor is called on that object before the heap memory allocation is returned a CStructType pStr new CStructType allocate a CStructType object on the dynamic heap b delete pStr return the dynamic memory allocation to the process c char pCs new char allocate an array of chars on the heap d delete pCs deallocate the entire array References C Survival Guide CCSE ?? Object Oriented Design Spring The C Programming Language Stroustrup Addison-Wesley Chap www ecs syr edu faculty fawcett handouts cse code basic basic cpp C Survival Guide CCSE ?? Object Oriented Design Spring C Classes declare class class cl public cl cl const cl f cl int val cl int access private int value Note names of formal parameters like f and val have no syntactic value and can be omitted default constructor copy constructor promotion constructor destructor accessor data member De ?ne class members cl cl value cl cl const cl f value f value cl cl int val value val cl cl int cl access return value create cl with value initiClized to zero create cl object as a copy of f create cl object with value val destroy cl object ?? does nothing provide read write access
Documents similaires
-
29
-
0
-
0
Licence et utilisation
Gratuit pour un usage personnel Attribution requise- Détails
- Publié le Jul 20, 2022
- Catégorie Creative Arts / Ar...
- Langue French
- Taille du fichier 62.9kB