Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Introduction to C++ Templates includes C++ Function Templates, Function Overloading, Function Template, Instantiating a Function, Summary of Three Approaches, Class Template, Instantiating a Class Template. | Introduction to C++ Templates C++ Function Templates C++ Class Templates Copyright 2006, The Ohio State University C++ Function Templates Approaches for functions that implement identical tasks for different data types Naïve Approach Function Overloading Function Template Instantiating a Function Templates Copyright 2006, The Ohio State University Approach 1: Naïve Approach create unique functions with unique names for each combination of data types difficult to keeping track of multiple function names lead to programming errors Copyright 2006, The Ohio State University Example void PrintInt( int n ) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void PrintChar( char ch ) { cout << "***Debug" << endl; cout << "Value is " << ch << endl; } void PrintFloat( float x ) To output the traced values, we insert: { PrintInt(sum); } void PrintDouble( double d ) PrintChar(initial); { PrintFloat(angle); } Copyright 2006, The Ohio State University Approach 2:Function Overloading (Review) • The use of the same name for different C++ functions, distinguished from each other by their parameter lists • Eliminates need to come up with many different names for identical tasks. • Reduces the chance of unexpected results caused by using the wrong function name. Copyright 2006, The Ohio State .