Absolute C++ (4th Edition) part 66

Absolute C++ (4th Edition) part 66. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | Function Templates 657 Display A Function Template 1 Program to demonstrate a function template. 2 include iostream 3 using std cout 4 using std endl 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Interchanges the values of variablel and variable2. The assignment operator must work for the type T. template class T void swapValues T variablel T variable2 T temp temp variable1 variable1 variable2 variable2 temp Compilers still have problems with templates. To be certain that your templates work on the widest selection of compilers place the template definition in the same file in which it is used and have the template definition precede all uses of the template. int main int integer1 1 integer2 2 cout Original integer values are integer1 integer2 endl swapValues integer1 integer2 cout Swapped integer values are integer1 integer2 endl char symbol1 A symbol2 B cout Original character values are symbol1 symbol2 endl swapValues symbol1 symbol2 cout Swapped character values are symbol1 symbol2 endl return 0 Sample Dialogue Original integer values are 1 2 Swapped integer values are 2 1 Original character values are A B Swapped character values are B A 658 Templates time the arguments are of type int and the other time the arguments are of type char. Consider the following function call from Display swapValues integer1 integer When the C compiler gets to this function call it notices the types of the arguments in this case int and then it uses the template to produce a function definition with the type parameter T replaced with the type name int. Similarly when the compiler sees the function call swapValues symbol1 symbol it notices the types of the arguments in this case char and then it uses the template to produce a function definition with the type parameter T replaced with the type name char. calling a Notice that you need not do anything special when you call a function that is defined with a function template you call it just as .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
2    814    2    01-06-2024
63    4    1    01-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.