tailieunhanh - Lecture Object oriented programming - Lecture no 25

In this chapter, we will present several nonnumerical recursive algorithms in this chapter. We will also discuss some criteria for deciding when to use recursion and when not to. All the recursive algorithms we provide in this chapter, other than those we use for explanation, are algorithms that should be written recursively. | CSC241: Object Oriented Programming Lecture No 25 Previous Lecture Example programs Publication company Two derived classes Book and Tape Polymorphic behavior: Virtual function Distance class Overloaded * operator Friend version of member function required one more argument Today’s Lecture Intro to Generic Programming Template Function template Class template Generic programming There are situations where same algorithms works for different data type Suppose a function that returns the maximum of two numbers Same algorithm (to find the max number) works with integers and real numbers Until now we have to write two separate function int max (int, int); float max (float, float); Through generic programming we can have just one function to find max of int or float or any other type of values Cont. Generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided | CSC241: Object Oriented Programming Lecture No 25 Previous Lecture Example programs Publication company Two derived classes Book and Tape Polymorphic behavior: Virtual function Distance class Overloaded * operator Friend version of member function required one more argument Today’s Lecture Intro to Generic Programming Template Function template Class template Generic programming There are situations where same algorithms works for different data type Suppose a function that returns the maximum of two numbers Same algorithm (to find the max number) works with integers and real numbers Until now we have to write two separate function int max (int, int); float max (float, float); Through generic programming we can have just one function to find max of int or float or any other type of values Cont. Generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameter So if max() function is called with int parameter, max function with int parameter is created In C++ generic programming is implemented with “templates Benefit of generic programming Reusability Value Max (Value1, Value2) { if( Value1 > Value2 ) return Value1; else return Value2; } y= Max(15, 67); int Max (int arg1, int arg2) { if( 15 > 67 ) return arg1; else return arg2; } y= Max(, ); int Max (float arg1, float arg2) { if( 15 > 67 ) return arg1; else return arg2; } Templates Templates make it possible to use one function or class to handle many different data types The template concept can be used in two different ways: with functions and with classes A simple function template Suppose we want to write a function that returns the absolute value of two numbers absolute value of a number is its value without regard to its sign: absolute value of 3 is 3 absolute value of –3 is also 3 absolute value of is absolute value of is Why Template .

TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
crossorigin="anonymous">
Đã 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.