m08_GNR - bEgInSlIdE 1 Chapter 8 The C+ Programming...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 1 The C++ Programming Language Generic Programming (templates) Chapter 8
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 2 The Principle of Little Numbers 0 > 1 : What you don't know cannot hurt you. Use Information hiding, encapsulation, table driven programming, modularity, etc. 1 > 2 : If the same thing occurs twice in the code, then it occurs once too many . . Software is constantly subject to change. As changes occur, maintaining two, almost identical, parts will become a nightmare . . For (ever) { Write Code; While (exist(similar parts)) { Extract identical portion; Rewrite code; } } A.k.a. ‘Refactoring’
Background image of page 2
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 3 TEMPLATES C++ provides a super macro mechanism that is identified by the keyword template . Templates can be thought of as MACROs for functions and classes , that need not be explicitly called by the programmer. Templates are controlled by the compiler (and not by the pre-processor). Template arguments are either class -types or const -expressions of a fixed type. • Arguments that are passed to template functions are checked for type-mismatch.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 4 Function Templates template < class T> T max(T a, T b) { return a>b?a:b; } int main() { int a = 3, b = 4; char c = „a‟, d = „b‟; cout << max(a,b); //o.k. cout << max(c,d); //o.k. cout << max(a,c); // Error!! return 0; } Declaration Usage Defining additional function int max( int , int ) will prevent the compilation error. operator > should be defined for class T No conversions in templates
Background image of page 4
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 5 • For each call to a function template, the compiler will try to generate an actual function with the appropriate prototype Overloading resolution for functions of the same name is done in this order: – Look for an exact match among (non-template) functions. – Look for an exact match that can be generated from a function template. – Look for the best match, using (in that order): » Trivial conversions (which are considered to be exact match ) » Promotions ( e.g, short to int ) » Standard conversions ( e.g, int to double ) » User-defined conversions ( e.g, double to complex ) Usually, every template-argument specified in the template-argument-list should be used in the argument-list of a function template. Function Templates (Cont.)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
bEgInSlIdE Copyright 1995 - 2007 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion 6 The function swap will work for any type T for which a copy constructor and operator= are defined Function templates: examples template < typename T> void { T temp = a; a = b; b = temp; } copy ctor operator= String s1, s2; swap(s1,s2); int x = 5, y = 10; swap(x,y);
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

m08_GNR - bEgInSlIdE 1 Chapter 8 The C+ Programming...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online