Miscellaneous C Topics CS2303 System Programming Concepts Slides

  • Slides: 29
Download presentation
Miscellaneous C++ Topics CS-2303 System Programming Concepts (Slides include materials from The C Programming

Miscellaneous C++ Topics CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) CS-2303, C-Term 2010 Miscellaneous C++ Topics 1

Outline – More topics from Ch 18 • Inline functions • Default Arguments •

Outline – More topics from Ch 18 • Inline functions • Default Arguments • Function Overloading • Function Templates CS-2303, C-Term 2010 Miscellaneous C++ Topics 2

Inline Functions • Advice to compiler that the body of the function may be

Inline Functions • Advice to compiler that the body of the function may be substituted for a call to that function • Insert the qualifier inline before return type of function • Example: – inline int max(int a, int b) { return (a>b) ? a : b; } CS-2303, C-Term 2010 Miscellaneous C++ Topics 3

Inline Functions (continued) • Reasons: – • Reduce function call overhead—especially for small functions

Inline Functions (continued) • Reasons: – • Reduce function call overhead—especially for small functions in inner loops • Takes advantage of compiler’s local optimizations • Trade-off of inline functions • Multiple copies of the function code are inserted in the program (possibly making the program larger) • The compiler may ignore the inline qualifier • Typically does so for all but the smallest functions CS-2303, C-Term 2010 Miscellaneous C++ Topics 4

Inline Function Example using avoids repeating std: : inline qualifier Complete function definition so

Inline Function Example using avoids repeating std: : inline qualifier Complete function definition so the compiler knows how to expand a cube function call into its inlined code. CS-2303, C-Term 2010 Miscellaneous C++ Topics 5

Inline Functions (continued) • Typical usage: – Member function of a class • •

Inline Functions (continued) • Typical usage: – Member function of a class • • Very simple, short, fast operations Especially in inner loops Especially set and get functions Saves the (non-trivial) overhead of invoking a method, creating activation record, etc. • Encourages cleaner, more readable code Widely used whenever you ask yourself about efficiency of a calling a function vs. accessing a class member directly • inline qualifier is strictly advisory CS-2303, C-Term 2010 Miscellaneous C++ Topics 6

Questions? CS-2303, C-Term 2010 Miscellaneous C++ Topics 7

Questions? CS-2303, C-Term 2010 Miscellaneous C++ Topics 7

Default Argument • Definition: – A default value to be passed to a parameter

Default Argument • Definition: – A default value to be passed to a parameter • When the function/method call does not specify an argument for that parameter • Example void prnt(int value, int base = 10); … prnt(100); //prints in base 10 prnt(100, 16); //prints in base 16 prnt(100, 8); //prints in base 8 prnt(100, 3); //prints in base 3 CS-2303, C-Term 2010 Miscellaneous C++ Topics 8

Default Arguments (continued) • Must be rightmost parameter(s) in parameter list of function •

Default Arguments (continued) • Must be rightmost parameter(s) in parameter list of function • Default value specified in function prototype • Usually in class header file • So compiler knows how to compile calls • If one argument is defaulted, all arguments to right of it must be defaulted also CS-2303, C-Term 2010 Miscellaneous C++ Topics 9

Default Argument Example void f(int a, double b, int c = 10, bool d

Default Argument Example void f(int a, double b, int c = 10, bool d = false); • • • f(100, 3. 14) f(100, 3. 14, 15, true) f(100, 3. 14, 10, true) // okay // no good // okay See also Fig 18. 8 of D&D CS-2303, C-Term 2010 Miscellaneous C++ Topics 10

Summary – Default Arguments • Simplest form of function overloading • Default values specified

Summary – Default Arguments • Simplest form of function overloading • Default values specified in header/prototype • Defaults applied from right to left CS-2303, C-Term 2010 Miscellaneous C++ Topics 11

Questions? CS-2303, C-Term 2010 Miscellaneous C++ Topics 12

Questions? CS-2303, C-Term 2010 Miscellaneous C++ Topics 12

Function Overloading • Multiple functions or methods with same name, but different types of

Function Overloading • Multiple functions or methods with same name, but different types of parameters and/or results • Compiler selects proper function to execute based on number, types, and order of arguments in the function call. • Commonly used to create several functions of the same name that perform similar tasks, but on different data types. Widely used for readable code CS-2303, C-Term 2010 Miscellaneous C++ Topics 13

Function Overloading Example Defining a square function for ints Defining a square function for

Function Overloading Example Defining a square function for ints Defining a square function for doubles Output confirms that the proper function was called in each case CS-2303, C-Term 2010 Miscellaneous C++ Topics 14

Compiling Overloaded Functions • Compiler mangles function names to create unique names for linker

Compiling Overloaded Functions • Compiler mangles function names to create unique names for linker – e. g. , – _square$qi – _square$qd • These become visible to programmer during linking, loading, and (sometimes) debugging! CS-2303, C-Term 2010 Miscellaneous C++ Topics 15

Another Function Overloading Example double sqrt(double x){ /* invoke hardware square root function of

Another Function Overloading Example double sqrt(double x){ /* invoke hardware square root function of a double >= 0 */ } complex sqrt(complex x){ /* algorithm to take square root of a complex number */ } CS-2303, C-Term 2010 Miscellaneous C++ Topics 16

Overloaded Constructors • Constructors are like functions or methods • May be overloaded •

Overloaded Constructors • Constructors are like functions or methods • May be overloaded • Useful for initializing a class object from various kinds of input CS-2303, C-Term 2010 Miscellaneous C++ Topics 17

Questions? CS-2303, C-Term 2010 Miscellaneous C++ Topics 18

Questions? CS-2303, C-Term 2010 Miscellaneous C++ Topics 18

Problem • What about the case when all of the overloaded functions have essentially

Problem • What about the case when all of the overloaded functions have essentially the same code, but for different types • Not good style to write a bunch of nearly identical code • Inefficient of programmer time • Various instances can get out of sync with each other • Some cases might be missed CS-2303, C-Term 2010 Miscellaneous C++ Topics 19

Solution – Function Templates • A more compact and convenient form of overloading. –

Solution – Function Templates • A more compact and convenient form of overloading. – Identical program logic and operations for each data type. CS-2303, C-Term 2010 Miscellaneous C++ Topics 20

Function Template • Written by programmer once • Essentially defines a whole family of

Function Template • Written by programmer once • Essentially defines a whole family of overloaded functions • Begins with the template keyword • Contains a template parameter list of formal type and the parameters for the function template are enclosed in angle brackets (<>) • Formal type parameters – Preceded by keyword typename or keyword class – Placeholders for fundamental types or user-defined types CS-2303, C-Term 2010 Miscellaneous C++ Topics 21

Function Template Example Using formal type parameter T in place of data type CS-2303,

Function Template Example Using formal type parameter T in place of data type CS-2303, C-Term 2010 Miscellaneous C++ Topics 22

Result • Whole Family of Overloaded Functions – All with the “same” source code

Result • Whole Family of Overloaded Functions – All with the “same” source code – All doing the same thing, but with different data types – Defined in one place • Common Programming Errors • Not placing keyword class or keyword typename before every formal type parameter of a function template • Writing < class S, T > instead of < class S, class T > ) is a syntax error CS-2303, C-Term 2010 Miscellaneous C++ Topics 23

Definition: – Function Template Specialization • Specialization: – the automatic generation of a new

Definition: – Function Template Specialization • Specialization: – the automatic generation of a new “overloaded function” from the template as needed • I. e. , whenever a function template is called with a particular type • Example for function template max with type parameter T called with int arguments – Compiler detects a max invocation in the program code. – int is substituted for T throughout the template definition. – This produces function-template specialization max<int> CS-2303, C-Term 2010 Miscellaneous C++ Topics 24

Specialization Example Function Template Example Compiler: – 1. Suspends what it was doing 2.

Specialization Example Function Template Example Compiler: – 1. Suspends what it was doing 2. Generates a new overloaded function from template maximum for parameter type int 3. Compiles this new function 4. Returns to this program and compiles a call to new function. Invoking maximum with int arguments CS-2303, C-Term 2010 Miscellaneous C++ Topics 25

Specialization Example (continued) Function Template Example Invoking maximum with double arguments Compiler does same

Specialization Example (continued) Function Template Example Invoking maximum with double arguments Compiler does same again, but for parameter type double Invoking maximum with char arguments And again, but for parameter type char CS-2303, C-Term 2010 Miscellaneous C++ Topics 26

Specialization (continued) • Each time that the compiler encounters a use of maximum with

Specialization (continued) • Each time that the compiler encounters a use of maximum with a type that it has not seen before – … it creates a new instance of the function maximum with new parameter types – With new mangled name! CS-2303, C-Term 2010 Miscellaneous C++ Topics 27

Function Templates Summary • Very important for – Code readability – Code reuse –

Function Templates Summary • Very important for – Code readability – Code reuse – Good coding style – Programmer efficiency • Foundation of other template features in C++ CS-2303, C-Term 2010 Miscellaneous C++ Topics 28

Questions? CS-2303, C-Term 2010 Miscellaneous C++ Topics 29

Questions? CS-2303, C-Term 2010 Miscellaneous C++ Topics 29