软件设计(II)教学课件:Chapter1-4 Basics of OOP and C++.pptVIP

  • 0
  • 0
  • 约9.78千字
  • 约 37页
  • 2021-11-30 发布于安徽
  • 举报

软件设计(II)教学课件:Chapter1-4 Basics of OOP and C++.ppt

Cost of functions Runtime overhead caused by function calls Pushing arguments and CPU registers into the stack Transferring control to and from a function Inline function (内联函数) A function not called by the caller. The compiler copies the function code in line at the point of each invocation. Inline Functions * inline type func name (para list) Used for short functions rather than long functions Copying long function codes lengthens the program A long “inline” function may be treated as a common function: the “inline” is ignored. The compiler makes such decisions Notes for Inline Functions * Parameters declared with default values Default Arguments (缺省参数) * type name(type para1, …, type parai=defaultvalue, …) { //Function body Statements; } Note: Default arguments must be declared at last int max(int x, int y=0, int z=0){ … } int max(int x, int y=0, int z){ … } E.g.: Program4.2 Const Arguments In c++, an argument to a function can be declare as constant. To protect the arguments from changes by the function. E.g. int strlen(const char *p); int length(const string s); Overloading (重载): two functions within one file with The same name, but Different parameter lists The types and/or the number of arguments Function Overloading * int max(int x, int y){ … } float max(float x, float y){ … } int max(int x, int y, int z){ … } Different parameter type Different parameter number Return value type does not matter!!! Implicit type conversion may be done in function invocation Type Conversion and Overloading * float max(float x, float y){ float z; if(x=y) z=x; else z=y; return z; } int main(){ int a=3, b=5; coutmax(a,b)endl; } long double double float unsigned long long unsigned int int Function version determination Which version of a overloaded function should be called for an invocation? First: the one with exactly matching parameter list Second: the one matching after typ

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档