190 likes | 228 Views
C++ Programming. University of Delaware Spring 2005 Session 6 Jim Brannen 04/2005. Tonight’s Agenda. References Inline Functions Function Overloading Function Default Arguments Const. References. New Feature in C++ References are aliases to an original named variable Example:
E N D
C++ Programming University of Delaware Spring 2005 Session 6 Jim Brannen 04/2005
Tonight’s Agenda • References • Inline Functions • Function Overloading • Function Default Arguments • Const
References • New Feature in C++ • References are aliases to an original named variable • Example: int a; a = 12; int *ptr = &a; int & ref_a = a; Int & ref_a1 = * ptr
References (Cont) • Why use references • Simplifies Code. – No more ptr->field • Can pass references and use them like pointers in functions –(Does away with pointer manipulation) • Efficient when passing arguments to functions
Observations on references • Can’t reference another reference • Can’t obtain the address of a reference • Can’t create an array of references • All references must be initialized unless • Member of a class • Return a value • Belong as function parameters
Observations on references (Cont) • Although references are similar to pointers they are not pointers and use the dot (.) operator to access members of an object • Should use const in arguments when you want the reference to be read-only Void show (const int & val) { cout << “ Value is “ << val; val = 10; // results in compile error }
Inline Functions • New Feature in C++ • Carried out by the C++ keyword inline
Inline (Cont) // example inline int max(int a, int b) { if (a < b) return a; else return b; Int main() { int high = max(10, 20); }
Inline Caveats • Not guaranteed by the compiler • Can’t be recursive • Can cause code “bloat” if used excessively since function call is replaced by the code when the function is used • Array declarations not allowed in inline functions
Inline (Cont) • Implicit inlining takes place when code is written as part of the class declaration, by default. Simple constructors and destructors make good fits. • Example follows
Implicit Inline Class example { private: int id_num; public: example ( int num) { id_num = num; } }:
Function Overloading • New Feature to C++ • Supports polymorphism (One interface, many uses) • Lets use of one function name using different arguments. The argument list is also called the “signature” of the function.
Function Overload Example int total(int a, int b) { return (a = b); } int total (int a, int b, int c) { return (a + b + c) }
Function Overloading (Cont) • Functions can have different return types only if the signatures are different: int add (int a, int b); double add (int c, int d); // not allowed
Default Arguments • New to C++ • Used to specify default values to arguments in functions, so we can omit them when the function is called. Example: int sum ( int a = 0, int b = 10, int c =5); y = sum(1,6); // returns 12 y = sum(); // returns 15 y = sum(6,1); // returns 12 y = sum (2,3,4); // returns 9
Default Arguments (cont) • Useful in class member functions such as constructors • Almost the same effect as overloading the member function • Always declared as part of the function prototype.
Default Arguments (Cont) • Can be provided for trailing arguments only • Examples: int f1 ( int a, int b = 1, int c = 5); // OK int f2 (int a = 2, int b = 1, int c); // Error int f3 (int a = 2, int b, int c = 5); // Error
Default Arguments (Cont) • Be careful not to “mask’ overloaded functions with default arguments • Examples: int f1 (int a, int b = 1, int c = 5); int f1 (int a, int b); int y = f1(1,2); // Which one is called?
Default arguments (Cont) • Answer to last slide: • Neither one is called. Compiler can’t decide which one. Will result in a compiler error.