Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Templates: Function and Class Templates, Operator Overloading, and Function Objects, Assignments of Computer Science

Various aspects of c++ templates, including function templates that define logic for multiple data types and class templates that create generic class patterns. Topics include function templates for finding maximum values, class templates for memorycell, operator overloading, and function objects. Assumptions and conventions are also discussed.

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-ais-2
koofers-user-ais-2 🇺🇸

10 documents

1 / 2

Toggle sidebar

Related documents


Partial preview of the text

Download C++ Templates: Function and Class Templates, Operator Overloading, and Function Objects and more Assignments Computer Science in PDF only on Docsity! 1 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<int> type, then compiler generates a corresponding function which Comparable is replaced by vector<int> type.  Similarly for vector<double>, vector<string> 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.  It’s a class template.  MemoryCell<int>, MemoryCell<string> are classes.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved