1 / 19

C++ Programming

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:

karenmann
Télécharger la présentation

C++ Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Programming University of Delaware Spring 2005 Session 6 Jim Brannen 04/2005

  2. Tonight’s Agenda • References • Inline Functions • Function Overloading • Function Default Arguments • Const

  3. 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

  4. 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

  5. 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

  6. 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 }

  7. Inline Functions • New Feature in C++ • Carried out by the C++ keyword inline

  8. 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); }

  9. 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

  10. 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

  11. Implicit Inline Class example { private: int id_num; public: example ( int num) { id_num = num; } }:

  12. 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.

  13. Function Overload Example int total(int a, int b) { return (a = b); } int total (int a, int b, int c) { return (a + b + c) }

  14. 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

  15. 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

  16. 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.

  17. 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

  18. 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?

  19. Default arguments (Cont) • Answer to last slide: • Neither one is called. Compiler can’t decide which one. Will result in a compiler error.

More Related