Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 9 лет назад пользователемРостислав Базупов
1 1/27 Chapter 9: Template Functions And Template Classes
2 2/27 Review Virtual Method is a way to make polymorphism. Syntax for virtual method: virtual ReturnType Method (parameters) ReturnType virtual Method (parameters) Compiler will determine the right method will be called using a virtual function table for every class which contains virtual methods. Pure virtual method is a virtual method but it has no code. Syntax for pure virtual method: virtual ReturnType Method (parameters)=0;
3 3/27 Review Abstract class is a result of so-high generation. Abstract class must have at least one pure virtual method. You can not create an object of abstract class but you can declare a pointer to it then, it points to an object of a concrete subclass.
4 4/27 Objectives Need of templates Template function Template Classes
5 5/ Need of templates int abs( int n) { return (n>=0) ? n:-n; } long abs( long n) { return (n>=0) ? n:-n; } float abs( float) { return (n>=0) ? n:-n; } double abs( double n) { return (n>=0) ? n:-n; } Can we implement only one function for finding the absolute value of any number?
6 6/27 Need of templates class Student { }; class StudentList { }; class Product { }; class ProductList { }; Can we implement only one class for a list of any type?
7 7/ What is a template? A name for a undefined-datatype Template allows you declare a family of relative functions or classes Template function is a function its data types of parameters are templates. Template class is a class containing template data members.
8 8/ Syntax for Defining Template template Example template
9 9/ Syntax for Template functions template T1 function ( T1 param1, T2 param2) { T1 result; return result ; } Attention: All templates must exist in parameters of function.
10 10/27 Demo 1: Default value of template parameter is not applied Template function is very powerful and flexible. Disadvantage of template function is calling funtion in an abnormal manner. This is the price must be paid when you want to get a general function.
11 11/27 Demo 2: Error template T2 must be used in parameter of the function sum
12 12/27 Demo 2: Error Templates must be declared separately for every template function. ?
13 13/27 Demo 3: Using template functions for processing 1D array
14 14/ Template classes Classes containing template data members. A way to implement general classes. Things you have to do when implementing a template class: –template must be declare before class declaration. –If a method is implemented out side the class, ClassName must be declared.
15 15/27 Demo 3: Class for Dynamic array list File: ar_list.cpp
16 16/27 Class for Dynamic array list… File: ar_list.cpp
17 17/27 Class for Dynamic array list… File: ar_list.cpp Implementing methods out side the class declaration.
18 18/27 Using ArrayList class (1)
19 19/27 Using ArrayList class (2) – File Student.cpp
20 20/27 Using ArrayList class (2)– File Student.cpp…
21 21/27 Using ArrayList class (2) File StuList.cpp…
22 22/27 Summary Template function: a way to create a family of related function. Type of parameter in template function will be determined at the place where the function is called. All template datatypes must be present in parameters of template function. Parameters with default value are not applied in template function because the compiler can not determine them at compile-time. A class is called template class if it contains data belongs to a template type.
23 23/27 Summary Syntax for implement a template function template DataType Func(T1 p1, T2 p2) { } Syntax for implementing a method out side the template class declaration template DataType ClassName :: method(params) { } Syntax for using a template class ClassName obj;
24 24/27 Exercises Implement a template class for a simple linked list. The class has the following methods: –Add an element to the beginning of the list (addFirst) –Add an element to the last of the list (addLast) –Insert an element to a known position (insert(T x, int index)) –Remove an element at a known position (remove(int)) –Remove an element (remove(T)) –Replace an element at a known position (replace(int,T)) –Remove all element ( remove(T)) –Clear all element (clear()) –Clear all known element (clear(T)) Using this class to write a program that will print out the binary format string of an input long number ( using linked-list stack). Using this class to write a program that will manage a list of employee
25 25/27 Exercises Implement a template class for an array. The class has the following methods: –Add an element to the beginning of the list (addFirst) –Add an element to the last of the list (addLast) –Insert an element to a known position (insert(int index)) –Remove an element at a known position (remove(int)) –Remove an element (remove(T)) –Replace an element at a known position (replace(int,T)) –Remove an element ( remove(T)) –Clear all element (clear()) –Clear all known element (clear(T)) –Operator [i] allows accessing the i th element Using this class to write a program that will accept an array then print out the array. Using this class to write a program that will accept an array then print out the array of books.
26 26/27 Exercises Implement a template class for a matrix. The class has the following method for: –inputting a matrix –printing a matrix –delete a row –delete a column –swap two rows –swap two columns –circular left shifting all columns –circular right shifting all columns –circular up shifting all rows –circular down shifting all rows –accessing an element at the row i and the column j using the operator (i,j) Using this class to write a program that will accept a matrix of double numbers then print out the matrix. Using this class to write a program that will accept a matrix of long numbers then print out the matrix.
27 27/27 Thanks
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.