templates - ObjectOrientedProgramming Alireza Abedinejad...

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

View Full Document Right Arrow Icon
1 Advanced Programming-Abedinejad Object Oriented Programming Alireza Abedinejad [email protected]
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Advanced Programming-Abedinejad 12.1 Introduction In this session we will discuss one of the more recent features of C++ for software reuse namely templates Direct support for generic programming: using types as parameters Used in C++ standard library to offer generality, flexibility and efficiency Template mechanism can be used both for functions and classes
Background image of page 2
3 Advanced Programming-Abedinejad 12.2 Function templates Overloaded function are normally used to perform similar operation on different types of data. Templates can be used instead of overloaded function. #include <iostream> using std::cout; using std::endl; // Defing template function instead of function overloading //int square( int x ) { return x * x; } //double square( double y ) { return y * y; } template <class T> T square(T x) { return x * x; } void main() { cout << "The square of integer 7 is " << square( 7 ) << "\nThe square of double 7.5 is " << square( 7.5 ) << endl; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Advanced Programming-Abedinejad 12.2 Function templates Function template definition begins with the template keyword followed by < formal parameters > Don’t forget to put class before every formal parameter of a function template Every formal parameter in the template definition must appear in the function’s parameter list at least once. Keep in mind that to gain software reusability using templates you have to consume memory!! Function templates benefit from C++ type checking in contrast to macros
Background image of page 4
Advanced Programming-Abedinejad 12.3 Overloading template functions The compiler uses only parameter lists to distinguish between functions of the same name. A function template itself may be overloaded in several ways: Same name and different parameters By non-template function with the same name and different parameters To perform matching, compiler first tries to find a precise match .If it fails, compiler checks the function templates to generate matches for function call. Lets take a look at previous example
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/21/2010 for the course COMPUTER S 002 taught by Professor Ramtinkhosravi during the Spring '10 term at Amirkabir University of Technology.

Page1 / 17

templates - ObjectOrientedProgramming Alireza Abedinejad...

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

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