Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI-383 Object-Oriented Programming & Design Lecture 25.

Similar presentations


Presentation on theme: "CSCI-383 Object-Oriented Programming & Design Lecture 25."— Presentation transcript:

1 CSCI-383 Object-Oriented Programming & Design Lecture 25

2 Chapter 18 Generics

3 Static Typing and Genericity  Dynamically-typed languages Any piece of code is polymorphic It is the programmer responsibility to ensure that no run- time error occurs  Statically-typed languages Programmer  Specify type of participants Compiler  Check that the code is used only with participants of the right type  Translate the code to target language using this assumption (results in more efficient code) In dynamically typed languages, these two tasks are done at run time  Genericity: Make the same piece of code usable for many different types, without compromising type safety and run time efficiency as in dynamic binding

4 Examples of Genericity  The Standard Template Library (STL) Developed by Alexander Stepanov In 1994, the ANSI/ISO C++ committee voted to adopt the STL as part of the standard C++ library The STL is a “living” library (i.e., it continues to be modified, expanded, improved, and reorganized) The STL is a robust collection of software components, most of which consist of template classes and template functions (thus the name of the library) The STL can be broken into three major categories  Containers  Iterators  Generic algorithms

5 Templates  A powerful C++’s software reuse feature  Templates enable programmers to specify, with a single code segment an entire range of related (overloaded) functions – called function-template specializations, or an entire range of related classes – called class- template specializations

6 Function Templates  Overloaded functions normally perform similar or identical operations on different types of data  If the operations are identical for each type, they can be expressed more compactly and conveniently using function templates  Based on the argument types, the compiler generates separate source-code functions (i.e., function-template specializations) to handle each function call appropriately

7 Function Templates  All function-template definitions begin with keyword template followed by a list of template parameters to the function template enclosed in angle brackets ( )  Each template parameter that represents a type must be preceded by either the keyword class or typename template  Note that keywords class and typename actually mean “any built-in or user-defined type”  Handout #13 Handout #13 In this example, the compiler creates three printArray specializations Notice that if T represents a user-defined type, there must be an overloading of operator<<

8 Templates  Although templates offer software-reusability benefits, remember that multiple function and class template specializations are instantiated in a program (at compile time), despite the fact that templates are written only once  These copies can consume considerable memory But this is not normally an issue because the code generated by the template is the same size as the code you would have written to produce the separate overloaded function

9 Overloading Function Templates  A function template may be overloaded Can provide other function templates that specify the same function name but different function parameters Can provide nontemplate functions with the same function name but different function arguments (e.g., nontemplate version of printArray that prints an array of character strings in neat, tabular format)

10 Overloading Function Templates  Compiler performs matching process to determine what function to call First, compiler finds all function templates that match function call and creates specializations based on the arguments in the function call Then, compiler finds all ordinary functions that match the function call The best match (from ordinary functions and function-template specializations) is chosen If an ordinary function and a specialization are equally good, the ordinary function is chosen Otherwise, if multiple matches, ambiguity and error message

11 Class Templates  It is possible to understand the concept of a “stack” (i.e., data structure with LIFO property) independent of the type of items being placed in the stack  This creates great opportunity for software reusability  We need means for describing notion of a stack generically and instantiating classes that are type- specific versions  C++ provides this capability through class templates

12 Class Templates  Class templates are called parameterized types because they require one or more type parameters to specify how to customize a “generic class” template  Handout #14 Handout #14 Note: Most C++ compilers require the complete definition of a template class to appear in the client source-code file that uses the template. For this reason, the implementations of the member functions of class templates are also defined in the header file Notice that the code in function main is almost identical for both the double Stack and the int Stack manipulations  Another opportunity for a function template!  Handout #15 Handout #15


Download ppt "CSCI-383 Object-Oriented Programming & Design Lecture 25."

Similar presentations


Ads by Google