Chapter 1 C++ Templates (Sections 1.6, 1.7) Templates

Chapter 1 C++ Templates (Sections 1.6, 1.7) Templates

Chapter 1 C++ Templates (Sections 1.6, 1.7) Templates Type-independent patterns that can work with multiple data types. Function Templates These define logic behind the algorithms that work for multiple data types. Class Templates

These define generic class patterns into which specific data types can be plugged in to produce new classes. Function Templates example Generic function to find a maximum value in a given vector If argument is a vector type, then compiler generates a corresponding function which Comparable is replaced by vector type. Similarly for vector,

vector etc. Assumption in this example: Operator< is defined in the Comparable. Hence assumptions made by the template much be clearly stated. Function Templates Usage Each call to findMax(Comparable a) on a different data type forces the compiler to generate a

different function using the template. Compiler will complain about findMax(v4) because IntCell class does not define operator< Class Template Example MemoryCell template can be used for any type Object. Assumptions

Object has a zero parameter constructor Object has a copy constructor Copy-assignment operator Convention Class templates declaration and implementation usually combined in a single file. It is not easy to separate them in independent files due to complex C++ syntax.

This is different from the convention of separating class interface and implementation in different files. Class Template Usage Example MemoryCell can be used to store both primitive and class types. Remember MemoryCell is not a class. Its a class template.

MemoryCell, MemoryCell are classes. Another example Operator Overloading Defines the meaning of operator< for Employee class. Output operator<< Define a public member

function print(ostream &out) Define a global nonclass function operator<< that calls print() Function Objects Objects whose primary purpose is to define a function Here findMax() accepts a Comparator parameter as a function object. Comparator assumed to

define the isLessThan(Comparator) function. There is a more formal way to define function objects in C++ (next slide). Function objects in C++ style Define operator() for CaseInsensitiveCompare class. Case-sensitive comparison can also be performed using STL function object less(...)

Matrices C++ library does not provide a matrix class Constructor Creates rows number of zero-sized vectors Resizes each vector to col elements Two types of [] operators

One for LHS that returns by reference Another for RHS that returns by constant reference So we have two very identical functions What makes their signatures different? Reading Assignment for next week 2.1, 2.2, 2.3, 2.4.1, 2.4.2, 2.4.3

Recently Viewed Presentations

  • Approaching and Dealing with End of Life Issues

    Approaching and Dealing with End of Life Issues

    Approaching and Dealing with End of Life Issues. ... The attempt made in recent decades by secularist thinkers to disengage the moral principles of western civilization from their scripturally based religious context, in the assurance that they could live a...
  • Promotional Concepts and Strategies

    Promotional Concepts and Strategies

    Types of Promotion in the Promotional Mix. Put the number of users and nonusers in the appropriate spaces on the graphic organizer. Why or why not do you use these sites? The Promotional Mix. The Concept of Promotional Mix.
  • Clinical Epilepsy -

    Clinical Epilepsy -

    Clinical Epilepsy American Epilepsy Society C-Slide *
  • Property&#x27;s Of 2D and 3D Shapes.! :]

    Property's Of 2D and 3D Shapes.! :]

    !2D FIGURES! Beyond this point will be just 2D shapes. You will review the Formulas of these 2D shapes and figures. !SQUARE! Formula: L x W Properties: 4 sides…all 90 degree angles, closed figure, 2 sets of parallel lines. Triangle...
  • Welcome to CKEC&#x27;s ELA Network Meeting!

    Welcome to CKEC's ELA Network Meeting!

    A tool to help teachers match targets, standards and assessments. Meant to be a repository of nationally normed bank items and teacher created items. As with any tool, use your professional judgment when applying items to your classroom. More items...
  • Chapter 8 Review -

    Chapter 8 Review -

    Saltwater and freshwater aquatic life zones cover almost three-fourths of the earth's surface, and oceans dominate the planet. The earth's aquatic systems provide important eco- logical and economic services. Human activities threaten biodiversity and disrupt ecological and economic services provided...
  • Trends and Standards for 3D Graphics for Handsets

    Trends and Standards for 3D Graphics for Handsets

    JSR184/239 Mascot Capsule X-FORGE Acceleon G30/G40™ PowerVR MBX / MBX Lite GoForce 4000/3000 IMAGEON 2300 And of course !! Mali50/100 ARM9 (200MHz), VFP, MBX ARM11 (330MHz), TMS310 (220MHz), VFP, MBX ARM11 (500MHz), MMDSP(ST), VFP, 3D Acc (ST) Hitachi semiconductor Mitsubishi...
  • Work and Simple Machines - Science Education at Jefferson Lab

    Work and Simple Machines - Science Education at Jefferson Lab

    Work and Simple Machines What is work? In science, the word work has a different meaning than you may be familiar with. The scientific definition of work is: using a force to move an object a distance (when both the...