Absolute C++ (4th Edition) part 67

Absolute C++ (4th Edition) part 67. 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. . | Class Templates 667 cout b contains for i 0 i 3 i cout b i cout endl cout c contains for i 0 i 3 i cout c i cout endl swapValues a b b 0 42 cout After swapping a and b n and changing b n cout a contains for i 0 i 3 i cout a i cout endl cout b contains for i 0 i 3 i cout b i cout endl cout c contains for i 0 i 3 i cout c i cout endl Class Templates Equal wealth and equal opportunities of culture. have simply made us all members of one class. Edward Bellamy Looking Backward2000 1887 As you saw in the previous section function definitions can be made more general by using templates. In this section you will see that templates can also make class definitions more general. SYNTAX FOR CLASS TEMPLATES The syntax details for class templates are basically the same as those for function templates. The following is placed before the template definition template class T 668 Templates type The type parameter T is used in the class definition just like any other type. As with parameter function templates the type parameter T represents a type that can be any type at all the type parameter does not have to be replaced with a class type. As with function templates you may use any nonkeyword identifier instead of T although it is traditional to use T. Display part 1 shows an example of a class template. An object of this class contains a pair of values of type T If T is int the object values are pairs of integers if T is char the object values are pairs of characters and so Once the class template is defined you can declare objects of this class. The declaration must specify what type is to be filled in for T. For example the following declares the object score so it can record a pair of integers and declares the object seats so it can record a pair of characters Pair int score Pair char seats The objects are then used just like any other objects. For example the following sets score to be 3 for the first team and 0 for the second team 3 .

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
Đã 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.