1/27 Chapter 9: Template Functions And Template Classes.

Презентация:



Advertisements
Похожие презентации
1/30 Chapter 8: Dynamic Binding And Abstract classes.
Advertisements

Unit II Constructor Cont… Destructor Default constructor.
1/13 Chapter 06- Implementing Operators in a class.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Sequences Sequences are patterns. Each pattern or number in a sequence is called a term. The number at the start is called the first term. The term-to-term.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
© Luxoft Training 2013 Java Collections API. © Luxoft Training 2013 Collections hierarchy.
Java Collections Framework (JCF) in Java Tutorial for students of universities Author: Oxana Dudnik.
S4-1 PAT328, Section 4, September 2004 Copyright 2004 MSC.Software Corporation SECTION 4 FIELD IMPORT AND EXPORT.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
1 © Luxoft Training 2012 Inner and anonymous classes.
© 2006 Cisco Systems, Inc. All rights reserved. MPLS v MPLS VPN Implementation Configuring VRF Tables.
Arrays Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
Yogesh Mehla Now concept of logic building is not so complex and not so simple. We will not work on how to make logic program in.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
Транксрипт:

1/27 Chapter 9: Template Functions And Template Classes

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/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/27 Objectives Need of templates Template function Template Classes

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/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/ 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/ Syntax for Defining Template template Example template

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/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/27 Demo 2: Error template T2 must be used in parameter of the function sum

12/27 Demo 2: Error Templates must be declared separately for every template function. ?

13/27 Demo 3: Using template functions for processing 1D array

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/27 Demo 3: Class for Dynamic array list File: ar_list.cpp

16/27 Class for Dynamic array list… File: ar_list.cpp

17/27 Class for Dynamic array list… File: ar_list.cpp Implementing methods out side the class declaration.

18/27 Using ArrayList class (1)

19/27 Using ArrayList class (2) – File Student.cpp

20/27 Using ArrayList class (2)– File Student.cpp…

21/27 Using ArrayList class (2) File StuList.cpp…

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/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/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/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/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 Thanks