OtherC++Features_第1页
OtherC++Features_第2页
OtherC++Features_第3页
OtherC++Features_第4页
OtherC++Features_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Other C+ FeaturesTrue reference parametersReadings: 3.3,3.5-3.7Default argumentsReadings: 4.1-4.7C+ Dynamic MemoryReadings 5.1-5.81OutlineTrue reference parametershow to define, how to call, how to useDefault argumentshow to define, how to call, order effects, legal valuesC+ Dynamic Memorynew, delet

2、e keywordsallocating/deleting single instanceallocating/deleting 1D arrayallocating/deleting 2D array2True Reference ParametersC+ has another way of passing parameters - true reference parametersA true reference parameter gives you a way to change a variable that is not part of a function without pa

3、ssing it as a pointerIn a function definition you indicate a function parameter by putting an & after the type (no *)Type& PnameIn a call to the function you give the name of the variableThe parameter becomes another way to refer to the variable3A Simple True Reference Parametervoid updateInt(int& n

4、um) / num becomes a synonym for the var / used in the function call num = num + 1;int X = 5;updateInt(X); / num becomes name for Xcout X endl; / Prints 6n4Reference to a PointerReference parameters useful for changing pointer vars (so you dont have to pass a pointer to a pointer)Put & after * with t

5、ypeExample:void InitList(LLStruct*& start) start = 0;LLStruct *ListStart;InitList(ListStart);5Reference to a StructureCan also pass a reference to a structure:struct TIME / more on this later int hours, minutes, seconds;void springForward(TIME& thetime) thetime.hours += 1;TIME currtime = 0, 0, 0;spr

6、ingForward(currtime);6Default Parameter ValuesIn C+ you can supply default values for parameterswhen calling such functions you can leave out arguments for some of these parametersthe default values of those parameters are used in place of argumentsthe function therefore can be called with a differe

7、nt number of arguments7Specifying Default ValuesFormat of parameter: Type Pname = DefaultValDefaultVal can be any appropriate expression (not necessarily a constant)Rules for includingmay have more than one default parameterdefault parameters must be the last parameters for functionthe order of the

8、default parameters matterscan leave out last default parameter, last two, last three, etc., but can not pick and choose (cant leave out last and third to last)8Default Examplevoid deffunc(int a, float b = 2.0, int c = 3, char d = A) cout a b c d endl;calls:deffunc(1); / Outputs 1 2.0 3 Andeffunc(1,4

9、.0); / Outputs 1 4.0 3 Andeffunc(1,5.0,6); / Outputs 1 5.0 6 Andeffunc(1,7.0,8,B); / Outputs 1 7.0 8 Bn9Can Use Objects as DefaultsObjects must be available (generally global)Example:void Read_To_Newline(istream& ins = cin) while (ins.get() != n);when called, cin is used as parameter ins unless an a

10、rgument is suppliedRead_To_Newline(); / Reads from keyboard (cin)Read_To_Newline(myin); / Reads from connected to10Using Expressions as DefaultsCan use a complex expression as a default valueas long as the expression is well-defined (i.e., generally involves global variables/functions)int default_ar

11、ray_length = 20;char *allocate_array(unsigned int size = (default_array_length * sizeof(char) + 1) 11Dynamic Memory AllocationSpace on the heap is allocated using the keyword new to create a new instance (similar to malloc)The space can be returned to the heap using the keyword delete (similar to fr

12、ee)There are no routines corresponding to calloc and realloc (though it is easy enough to write your own version of calloc)12Allocating a Single ElementUse new Type to create element of type TypeExamples:int* iptr = new int;float* fptr = new float;char* cptr = new char;int* iptrptr = new int*;each o

13、f these variables points to a new element of the appropriate type (a lot easier than malloc, eh?)can also use new during code:int* iptr2;iptr2 = new int; / As opposed to/ iptr2 = (int *) malloc(sizeof(int);13Initializing the Resulting SpaceUsing the basic format (new Type), the resulting space is no

14、t initializedIf you add an empty pair of parentheses (), the space is initialized to 0If you add a pair of parentheses with an appropriate value in between (val), the space is initialized to valExamplesint i1ptr = new int; / new space, ? valint i2ptr = new int(); / new space, 0 valint i3ptr = new in

15、t(42); / new space, 42 val14Deleting an InstanceUse delete keyword followed by pointer to space allocated on heap:delete pointer;Examples:int* iptr = new int;float *fptr = new float;int* iptrptr = new int*;delete iptr; / free space iptr connected todelete fptr;delete iptrptr;15Allocating a 1-Dimensi

16、onal ArrayUse square brackets, size after type in new:new TyperowsVariable should be a pointer to type TypeExample:int size = 10;int* iarray = new intsize;float* farray = new floatsize*2;int* iptrarray = new int*size+1;error to put parentheses around type16Releasing 1D ArrayTo release 1-dimensional

17、array use delete, but put between keyword delete and pointer:delete aptr;The brackets inform C+ you are giving back a group of memoryExample:int* iarray = new int10;delete iarray;17Allocating Space for StringUse new to allocate space for length of string plus one extra for delimiterExample:char *bas

18、estr = “hello”;char *copystr;copystr = new charstrlen(basestr)+1;strcpy(copystr,basestr);18Allocating 2-Dimensional ArrayUse technique similar to what we did in C:Type* twodname;twodname = new Type*dimension1;for (int d1 = 0; d1 dimension1; d1+) twodnamed1 = new Typedimension2;Releasing array:for (i

19、nt d1 = 0; d1 dimension1; d1+) delete twodnamed1;delete twodname;192D Array Example int *A; A = new int*5; for (int i = 0; i 5; i+) Ai = new int8; for (int j = 0; j 8; j+) Aij = i + j; for (int i = 0; i 5; i+) for (int j = 0; j 8; j+) cout setw(3) Aij; cout endl; for (int i = 0; i 5; i+) delete Ai; delete A;20Example: Array of Strings#include #include #include #include #include void main() const int BUFSIZE = 256; int maxlines = 32; int numlines = 0; ifstream fin; ofstream fout;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论