templates - Alireza Abedinejad [email protected] Advanced...

Info icon This 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]
Image of page 1

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

View Full Document Right 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
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; }
Image of page 3

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

View Full Document Right 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
Image of page 4
5 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
Image of page 5

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern