TEMPLATES - TEMPLATES In this chapter, you will: Learn...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: TEMPLATES In this chapter, you will: Learn about templates Explore how to construct function templates and class templates FUNCTION OVERLOADING Overloading a function refers to several functions with the same name, but different parameters. The types of parameters determine which function to be executed. int largerInt (int x, int y); char largerChar(char first, char second); double largerFloat(double u, double v); newString largerString(newString first,newString second); int larger(int x, int y); char larger(char first, char second); double larger(double u, double v); newString larger(newString first,newString second); Function overloading is used when we have the same action for different sets of data. However, for function overloading to work, we must give the definition of each of the functions. TEMPLATES By using templates we write a single code for a set of related functions- called function template and related classes- called class template. The syntax for templates is: template <class Type> declaration; where Type is the type of the data and declaration is either a function declaration or a class declaration. In C++, template is a reserved word. The word class in the heading refers to any user-defined type or built in type. Type is referred as a formal parameter to the template. Just as variables are parameters to functions, types (that is, data types) are parameters to templates. Function Templates The syntax for function template is template <class Type> function definition; where Type is referred to as a formal parameter of the template and it is used to specify the type of the parameters to the function and the return type of the function, and to declare variables within the function. template <class T> T larger(T x, T y) { if(x >= y) return x; else return y; } The statement cout << larger(5,6) << endl; is a call to the function template larger. Since 5 and 6 are of the type int, data type int is substituted for T and the compiler generates the appropriate code. In the function template definition if we omit the body of the function, the function template then, as usual, is the prototype. Example 8 #include <iostream> #include "myString.h" using namespace std; template <class Type> Type larger(Type x, Type y); int main() { cout << "Line 1: Larger of 5 and 6 = " << larger(5,6) << endl; //Line 1 cout << "Line 2: Larger of A and B = " << larger('A','B') << endl; //Line 2 cout << "Line 3: Larger of 5.6 and 3.2 = " << larger(5.6,3.2) << endl; //Line 3 newString str1 = "Hello"; //Line 4 newString str2 = "Happy"; //Line 5 cout << "Line 6: Larger of " << str1 << " and " << str2 << " = " << larger(str1, str2) << endl; //Line 6 return 0; }...
View Full Document

This note was uploaded on 04/18/2008 for the course CS 201 taught by Professor Markhieber during the Spring '08 term at University of Missouri-Kansas City .

Page1 / 9

TEMPLATES - TEMPLATES In this chapter, you will: Learn...

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

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