Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3330 Object-oriented Programming in C++

Similar presentations


Presentation on theme: "COP 3330 Object-oriented Programming in C++"— Presentation transcript:

1 COP 3330 Object-oriented Programming in C++
Template Spring 2019

2 Function Template A function template can perform the same algorithm for different types without specifying the types in advance Example: a function that computes the maximum for three different integers int maximum(int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; }

3 Function Template However, this function only works for int variables (or types that can be automatically converted to int) What if we wanted to compare variables with the type double? Do we have to write a new function? A function template allows this problem to be solved easily, with only one function

4 Function Template template<class T> T maximum(T value1, T value2, T value3) { T maximumValue = value1; if (value2 > maximumValue) maximumValue = value2; if (value3 > maximumvalue) maximumValue = value3; return maximumValue; }

5 How to Use? int main() { int int1, int2, int3; cout << “Input three integer values: “; cin >> int1 >> int2 >> int3; cout << “The maximum integer value is: “; << maximum(int1, int2, int3); double double1, double2, double3; cout << “\n\nInput three double values: “; cin >> double1 >> double2 >> double3; cout << “The maximum double value is: “; << maximum(double1, double2, double3); char char1, char2, char3; cout << “\n\nInput three characters: “; cin >> char1 >> char2 >> char3; cout << “The maximum character value is: “ << maximum(char1, char2, char3); return 0; }

6 Another Example This is a function template that prints the contents of the array template<class T> void printArray(const T*array, const int count) { for (int j = 0; j < count; j++) cout << array[j] << “ “; cout << endl; } This function will work on arrays of ANY class type when an appropriate insertion operator << has been overloaded

7 How to Use? int main() { const int aCount = 5; const int bCount = 7; const int cCount = 6; int a[aCount] = {1, 2, 3, 4, 5}; double b[bCount] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; char c[cCount] = “HELLO”; cout << “Array a contains: “ << endl; printArray(a, aCount); cout << “Array b contains: “ << endl; printArray(b, bCount); cout << “Array c contains: “ << endl; printArray(c, cCount); return 0; }

8 Class Template Suppose we want to design a class that uses array-bases storage to maintain a list of integers With class templates, we can make this class work with different types without altering and recompiling code To make a class into a template, prefix the class definition with the syntax template<class T> T is a type parameter, and it can have ANY name When the class is instantiated, we fill in an appropriate type Use the same prefix on the definitions of the member functions

9 Class Template For member functions, the name of the class will be
className<T>::functionName Also, in the main program, we must #include the actual definition file, in addition to the class declaration The compiler creates a different version of the class for each type that is used Thus, either the entire class should be written in the header file, OR the .cpp file should be included #include “templatelist.cpp”

10 Code Review

11 Questions


Download ppt "COP 3330 Object-oriented Programming in C++"

Similar presentations


Ads by Google