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

bài giảng chú giải trình hướng đối tượng bài 3

44 309 0

Đ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 44
Dung lượng 737 KB

Nội dung

UNIT-III Template  Easily create a large range of related functions or classes  Function template - the blueprint of the related functions  Template function - a specific function made from a function template  Class templates  Allow type-specific versions of generic classes  Syntax for class template: template <class T> class ClassName{ definition }  Need not use "T", any identifier will work Function templates  Syntax: template<class type> function_declaration; -Or- template<typename type> function_declaration;  Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type declaring function templates  template <class identifier> function_declaration;  template <typename identifier> function_declaration; The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way. example // function template #include <iostream> using namespace std; template <class T> T GetMax (T a, T b) { T result; result = (a>b)? a : b; return (result); } Continue…. int main () { int i=5, j=6, k; long l=10, m=5, n; k=GetMax<int>(i,j); n=GetMax<long>(l,m); cout << k << endl; cout << n << endl; return 0; } Class templates #include <iostream> using namespace std; template <class T> class mypair { T a, b; public: mypair (T first, T second) { a=first; b=second; } T getmax (); }; Cont… template <class T> T mypair<T>::getmax () { T retval; retval = a>b? a : b; return retval; } Cont… int main () { mypair <int> myobject (100, 75); cout << myobject.getmax(); return 0; } Introduction to Exception Handling • An exception exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing • Without exception handling exception handling – When an exception occurs, control goes to the operating system, where typically • an error message is displayed • the program is terminated • With exception handling – Programs are allowed to trap exceptions – There is a possibility to fix the problem and continuing execution [...]... divide by zero 23 if ( denominator == 0 ) 24 throw DivideByZeroException(); // terminate function 25 26 // return division result 27 return static_cast< double >( numerator ) / denominator; 28 } // end function quotient If the denominator is zero, throw 29 a DivideByZeroException object 30 int main() 31 { 32 int number1; // user-specified numerator 33 int number2; // user-specified denominator 34 double... main() 31 { 32 int number1; // user-specified numerator 33 int number2; // user-specified denominator 34 double result; // result of division 35 36 cout > number1 >> number2 ) { // try block contains code that might throw exception // and code that should not execute if... using namespace std; class ListException: public exception{ public: ListException (const string& message = “”) : exception(message.c_str()){} }; // end ListException Exceptions Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // A simple exception-handling example that checks for // divide-by-zero exceptions #include Define new exception class (inherit from exception) #include

Ngày đăng: 24/10/2014, 16:24

TỪ KHÓA LIÊN QUAN

w