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

提交評論