Lecture Programming in C++ - Chapter 17: Templates

On completion of this chapter students will know how to: Function templates; class templates; three types of sequences containers are vector, deque and list; basic components of STL are iterators, algorithms and containers; STL has both sequence and associative containers. | Chapter 17 – Templates Function Templates Express general form for a function Example: template for adding two numbers Lesson template Type sum (Type a, Type b) { return (a + b); } Function Templates Called in same manner as ordinary function Permissible to have both generic data types and ordinary data types Treated similar to overloaded function Need to be careful that function call data types compatible with function bodies Useful for operations that apply to many data types Lesson Overloaded Function Templates Cannot replace overloaded functions Perform same operations for each different data type Can vary number of arguments Specify more than one type of argument Distinguished by number or distribution of types of arguments Lesson Class Templates: Example Lesson template class Class1 { private: Type value; public: Class1 ( ); void set_value (Type); Type get_value ( ); }; Name of class Data member Constructor Member functions Class . | Chapter 17 – Templates Function Templates Express general form for a function Example: template for adding two numbers Lesson template Type sum (Type a, Type b) { return (a + b); } Function Templates Called in same manner as ordinary function Permissible to have both generic data types and ordinary data types Treated similar to overloaded function Need to be careful that function call data types compatible with function bodies Useful for operations that apply to many data types Lesson Overloaded Function Templates Cannot replace overloaded functions Perform same operations for each different data type Can vary number of arguments Specify more than one type of argument Distinguished by number or distribution of types of arguments Lesson Class Templates: Example Lesson template class Class1 { private: Type value; public: Class1 ( ); void set_value (Type); Type get_value ( ); }; Name of class Data member Constructor Member functions Class Template Allows creation of object of class and use the data type of choice Syntax to declare object Class1 ob; Indicates the is type double Lesson Mechanics of Class Templates Declaration for object using class template Causes memory reserved for all data members Causes instructions to be generated and stored for all function members If another object with same bracketed data type declared New memory reserved, but no new function instructions Lesson Friends of Class Templates Four cases (assuming single type parameter) Ordinary function friend of each template class instantiated from class template Ordinary class friend of each template class instantiated from class template Template function – only if type parameter for function and class same Template class – only matching type class is friend Lesson Sequence Containers Designed to directly control position of element within container Three containers vector deque list Dynamic memory allocation used to .

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
6    81    2    05-05-2024
153    85    4    05-05-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.