SlideServe
Menu
Préférences
© SlideServe · v2026

C++ Programming

karenmann avatar
19 diapositives · 325 Views · Jan 07, 2020

Dive deep into advanced C++ programming concepts such as inline functions, function overloading, default arguments, and references. Explore the nuances of each feature and learn how to leverage them effectively in your code.

C++ Programming - slide 1
01 / 19C++ Programming
1 / 17

Télécharger la présentation

C++ Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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.