1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình hướng đối tượng - Chương 5 doc

40 239 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 40
Dung lượng 132 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Function templates• A function template is a pattern for creating a family of similar functions.. • If you need two or more functions with parameter types, you can write a function te

Trang 1

Chương 5

TEMPLATES

Trang 2

Tài liệu đọc

Trang 4

• According to the dictionary, a template

is a pattern or guide used to replicate

an object e.g a biscuit cutter

Trang 5

Function templates

• A function template is a pattern for creating a

family of similar functions

• If you need two or more functions with

parameter types, you can write a function

template that the compiler will use to generate

Trang 6

Function templates

• A function template has the general form

template <class T, >

returnType functionName(parameterList) {

}

Trang 7

Function templates (I)

• The keyword template indicates that what follows

is a function template, not an actual function

• The notation <class T, > is the template

parameter list

Trang 8

Function templates (II)

• The keyword class indicates that T is a

generic type i.e a placeholder for a data type used by the function

• The identifier T appears throughout the

function definition wherever this type needs

to be written.

Trang 9

Generated functions

• When the function template is called, the

compiler deduces the type of the actual

argument and substitutes it for the generic type T , creating what is known as a

generated function

• The act of generating a function is referred

Trang 12

Explicitly specifying the type

• The actual type that is substituted for a template's

generic type can be explicitly specified in a function call by writing it between angle brackets immediately after the function name

Trang 13

Instantiating with different types

• You cannot instantiate a function template if the types of arguments in your function call fail to match the template's expectations.

int r = 3;

double s = 4.4;

swap(r, s); // Error

Trang 14

Mixing generic types and actual types

• A function template's parameter list may

contain a mixture of generic and actual types.

Trang 15

Making templates available to the

compiler

• Do not separate a function template's

declaration from its definition by placing

them in separate header and implementation files

• Place a function template's declaration and

its definition in the header file

Trang 16

Overloading a function template

• If a function template cannot handle all of

the instantiations that you want it to

perform, you may wish to overload it

• If you explicitly overload a function

template, then the overloaded function will take precedence over the generated

function in the event of an exact match

Trang 17

// Swaps two character strings

// The strings are large enough for the swap to //occur

template<>

void swap(char* first, char* second)

{

int maxLength; // Length of longest string

// Find length of longest string

if (strlen(first) >= strlen(second))

maxLength = strlen(first);

else

Trang 18

// Allocate memory for temporary string

char* temporary = new char [maxLength + 1]; assert(temporary != 0);

Trang 19

Class templates

• A class template is a pattern for creating

a family of similar classes

• If you need two or more classes with

identical members that differ only in their parameter types, you can write a class template that the compiler will use

to generate the definitions of the actual

Trang 20

• The keyword template indicates that what

follows is a class template, not an actual class.

Trang 21

Class templates

• The notation <class T, > is the template parameter list

• The keyword class indicates that T is

generic type i.e a placeholder for a data type used by the class

• The identifier T appears throughout the

class definition wherever this type needs

to be written

Trang 22

Generated classes

• When an object of a class template is

declared, the compiler instantiates the

template, creating what is known as a

generated class

• The declaration of a generated class has the

general form

className<typeName, > objectName;

Trang 23

Member functions of a generated class

• The member functions of a class template

are function templates, and must be

written as such

• Each member function definition that

appears outside the class definition must

begin with the prefix

Trang 24

Member functions of a generated class

• In addition, the member function's name

must be qualified with the parameterised class name

followed by the scope resolution operator.

Trang 25

// Header file for the Stack class,

Trang 26

public:

Stack() : numItems(0)

{ }

void push(T item);

T pop();

};

Example 3 (stack.h) 2

Trang 27

// Push item onto stack

Trang 28

// Pop item off stack

Trang 29

#include "stack.h"

#include <iostream>

int main(void)

{

Stack <int> intStack;

Stack <char> charStack;

Trang 30

Friends of class templates

• Class templates can have friend functions.

• A friend function that doesn't use template

parameters is universally a friend of all

instances of the class template

• A friend function that uses template

parameters is only a friend of the particular class for which it is instantiated

Trang 31

template <class T>

class Matrix

{

//

friend void grid();

friend ostream& operator<<(ostream& out,

Trang 32

Inheriting Template Classes

Template classes can be inherited according to the standard inheritance guidelines

Inheritance can use two approaches (both very common):

The derived class is not a template The actual type will be defined when inheriting

class ExtendedIntArray : public Array<int>

The derived class is by itself a template class No need to specify the type when inheriting.

template<class T>

class ExtendedArray : public Array<T>

Introduction

Trang 33

Inheriting Template Classes

If while inheriting the template class, we specify all the parameters of the

template class, we will actually create a new regular (non-template) class.

 Example: ExtendedIntArray

A Class Derived from Template Class

Trang 34

Inheriting Template Classes

 Example: extintarray.cpp

 Example: temptemp.cpp

A Template Class Derived from a Template Class

I'm class A<int>

I hold int

The inner value is: 9

I'm class B<class A<int> >

I hold class A<int>

The inner value is: I'm class A<int>

I hold int

The inner value is: 9

I'm class A<int>

I hold int

Trang 35

Exercise 1

Implement a generic Find() function, which gets:

 a pointer to the beginning of an array

 a pointer to the end of an array

 the value to search for

 The function returns the address of the first items in the array that is equal to the given value or the address of one location after the last item if the value was not found.

int array[ ] = {3,2,5,7,2,8,11};

int* j = Find (array, array + 6, 7);

}

Trang 36

Exercise 2.1

 Implement a class called Sqr which will include only one thing:

will assign to it its square value.

Trang 37

Exercise 2.2

 Implement a general DoIt fucntion which gets:

 a pointer to the beginning of an array

 a pointer to the end of an array

Trang 38

 DoIt() is completely general:

It doesn’t know the array type.

It doesn’t know the action it is doing

Trang 39

Food for Thoughts

Imagine a template class

We wish that all instances of MyClass will be friends

 friend MyClass<T> ???

 friend MyClass<G> ???(and than template<class T, class G>)

What if a regular (non-template) class A wishes to give friendship to all instances of MyClass?

 What’s the meaning of all that???

Trang 40

Hỏi và Đáp

Ngày đăng: 09/07/2014, 00:20

TỪ KHÓA LIÊN QUAN

w