ICOM 4015 Advanced Programming Lecture 7 Procedural Abstraction
ICOM 4015 Advanced Programming Lecture 7 Procedural Abstraction V Reading: Chapter 3 Prof. Bienvenido Velez 10/7/2020 ICOM 4015 1
Procedural Abstraction VI Outline • Function overloading • Function templates 10/7/2020 ICOM 4015 2
Function Overloading SQR Function Family int. Sqr (int x) { return x * x } long. Sqr(long x) { return x * x; } Without overloading float. Sqr(float x) { return x * x } int sqr (int x) { return x * x } long sqr(long x) { return x * x; } With overloading float sqr(float x) { return x * x } 10/7/2020 ICOM 4015 3
Function Templates SQR Function Family int sqr (int x) { return x * x } long sqr(long x) { return x * x; } With overloading float sqr(float x) { return x * x } template <class T> T sqr (T x) { return x * x } 10/7/2020 With templates ICOM 4015 4
SQR’aring different types // Standard C++ header files #include <iostream> #include <iomanip> // Forward definitions of local auxiliary functions template <class T> T sqr(T x); // Main function int main() { cout << " i" << " sqr(i)" << " sqr(float(i))" << " sqr(double(i))" << endl; for (int i=0; i<10; i++) { cout << setw(16) << i << setw(16) << sqr(i) << setw(16) << sqr(float(i)) << setw(16) << sqr(double(i)) << endl; } } // Local auxiliary functions template <class T> T sqr(T x) { return x * x; } Templates can reduce code duplication dramatically 10/7/2020 ICOM 4015 5
Output [bvelez@amadeus] ~/icom 4015/lec 09 >>sqr i sqr(i) sqr(float(i)) 0 0 0 1 1 1 2 4 4 3 9 9 4 16 16 5 25 25 6 36 36 7 49 49 8 64 64 9 81 81 [bvelez@amadeus] ~/icom 4015/lec 09 >> 10/7/2020 ICOM 4015 sqr(double(i)) 0 1 4 9 16 25 36 49 64 81 6
Anatomy of a Function Template T is a type parameter template <class T> function Inside this function T represents any type Templates are C++’s implementation of Parametric Polymorphism 10/7/2020 ICOM 4015 7
Example 2 // Standard C++ header files #include <iostream> #include <iomanip> // Forward definitions of local auxiliary functions template <class T> void swap(T& a, T& b); template <class T> void do. Swap(T a, T b); // Main function int main() { cout << "***** do. Swap(1, 0)" << endl; do. Swap(1, 0); cout << endl << "***** do. Swap(1. 0/3. 0, 2. 0/3. 0)" << endl; do. Swap(1. 0/3. 0, 2. 0/3. 0); cout << endl << "***** do. Swap(true, false)" << endl; do. Swap("hello", "world"); } // Local auxiliary functions template <class T> void do. Swap(T a, T b) { T x = a; T y = b; cout << "x = " << x << " y = " << swap(x, y); cout << "swap(x, y)" << cout << "x = " << x << " y = " << } template <class T> void swap(T& a, T& b) { T temp = a; a = b; b = temp; 10/7/2020 ICOM 4015 } y << endl; y << endl; Variable declaration of type T 8
Example 2 Output [bvelez@amadeus] ~/icom 4015/lec 09 >>swap ***** do. Swap(1, 0) x = 1 y = 0 swap(x, y) x = 0 y = 1 swap(x, y) x = 1 y = 0 ***** do. Swap(1. 0/3. 0, 2. 0/3. 0) x = 0. 333333 y = 0. 666667 swap(x, y) x = 0. 666667 y = 0. 333333 swap(x, y) x = 0. 333333 y = 0. 666667 ***** do. Swap(true, false) x = hello y = world swap(x, y) x = world y = hello swap(x, y) x = hello y = world 10/7/2020 ICOM 4015 9
Function Overloading Summary of Concepts • Related functions can be grouped under a common name • Overloaded functions may have different return types, but must have different parameters. • The importance of overloading will become clearer when we get into classes and object-oriented programming 10/7/2020 ICOM 4015 10
Function Templates Summary of Concepts • Programmer declares one function parameterized over some type T • Compiler instantiates potentially many functions for all the different argument types provided among all function calls • Instances must be well typed, that is, all objects should only be used according to their types. 10/7/2020 ICOM 4015 11
- Slides: 11