Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.

Similar presentations


Presentation on theme: "C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered."— Presentation transcript:

1 C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered

2 C++ Programming: From Problem Analysis to Program Design, Second Edition2 This pointer and friend functions Two terms introduced in chapter 14: 1.This pointer 2.Friend functions You are NOT responsible to understand these concepts for exams You will not be required to use them in programs

3 C++ Programming: From Problem Analysis to Program Design, Second Edition3 Pointer this Every object of a class maintains a (hidden) pointer to itself called this

4 C++ Programming: From Problem Analysis to Program Design, Second Edition4 Example of this pointer class sample { public: ……. void funca(); private: ……. int count; };

5 C++ Programming: From Problem Analysis to Program Design, Second Edition5 Referencing private data funca can refer to count by: count = 0; or this -> count = 0; The simpler syntax can be used in most cases, and in all cases in CS215

6 C++ Programming: From Problem Analysis to Program Design, Second Edition6 Friend Functions of Classes friend function: a function defined outside the scope of a class A friend is a nonmember function −However, has access to private data members The concept of a friend function breaks down the wall of privacy for a class USE VERY CAREFULLY DO NOT USE IN THIS CLASS

7 C++ Programming: From Problem Analysis to Program Design, Second Edition7 Operator Overloading For your classes, you can overload operators (==,, etc.) to make the client code clearer The string class does this: if (string1 == string2) If you do not use operator overloading, you have to supply a public member function: if (string1.compare(string2)) You are not responsible for operator overloading in this class

8 C++ Programming: From Problem Analysis to Program Design, Second Edition8 Introduction to Templates Compare: 1.Generic functions 2.Function overloading 3.Templates

9 C++ Programming: From Problem Analysis to Program Design, Second Edition9 Generic Functions Functions in which the actions or steps are defined, but the data types of the items being manipulated are not.

10 C++ Programming: From Problem Analysis to Program Design, Second Edition10 Example of a Generic Function void PrintInt( int n ) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void PrintChar( char ch ) { cout << "***Debug" << endl; cout << "Value is " << ch << endl; } void PrintFloat( float x ) { } void PrintDouble( double d ) { } sum = alpha + beta + gamma; PrintInt(sum); PrintChar(initial); PrintFloat(angle); To output the traced values, we insert:

11 C++ Programming: From Problem Analysis to Program Design, Second Edition11 Function Overloading The use of the same name for different C++ functions, distinguished from each other by their parameter lists Eliminates need to come up with many different names for identical tasks. Reduces the chance of unexpected results caused by using the wrong function name.

12 C++ Programming: From Problem Analysis to Program Design, Second Edition12 Function Overload Example void Print( int n ) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void Print( char ch ) { cout << "***Debug" << endl; cout << "Value is " << ch << endl; } void Print( float x ) { } Print(someInt); Print(someChar); Print(someFloat); To output the traced values, we insert:

13 C++ Programming: From Problem Analysis to Program Design, Second Edition13 Templates A C++ language construct that allows the compiler to generate multiple versions of a function from one template function When they only differ in data type

14 C++ Programming: From Problem Analysis to Program Design, Second Edition14 Comparison Generic functions: separate function for each data type, each function has a unique name Function overloading: separate function for each data type, using the same name Function template: One function written for all data types, compiler generates separate functions per data type used

15 C++ Programming: From Problem Analysis to Program Design, Second Edition15 Template Example Templates used when functions only differ by data type Example: dynamic array Using a template, you can write one dynamic array class and use it for arrays of any data type

16 C++ Programming: From Problem Analysis to Program Design, Second Edition16 Example of a Function Template template void Print( SomeType val ) { cout << "***Debug" << endl; cout << "Value is " << val << endl; } Print (sum); Print (initial); Print (angle); To output the traced values, we insert: Template parameter Template argument

17 C++ Programming: From Problem Analysis to Program Design, Second Edition17 Generic Functions, Function Overload, Template Functions Generic Function Different Function Definitions Different Function Names Function Overloading Different Function Definitions Same Function Name Template Functions One Function Definition (a function template) Compiler Generates Individual Functions

18 C++ Programming: From Problem Analysis to Program Design, Second Edition18 Example of a Class Template template class GList // Dynamic array class { public: bool IsEmpty() const; bool IsFull() const; int Length() const; void Insert( /* in */ ItemType item ); void Delete( /* in */ ItemType item ); bool IsPresent( /* in */ ItemType item ) const; void Print() const; GList(); // Constructor private: int length; ItemType data[MAX_LENGTH]; }; Template parameter

19 C++ Programming: From Problem Analysis to Program Design, Second Edition19 Client use of a Class Template // Client code GList list1; GList list2; GList list3; list1.Insert(356); list2.Insert(84.375); list3.Insert("Muffler bolt"); To create lists of different data types GList_int list1; GList_float list2; GList_string list3; template argument Compiler generates 3 distinct class types

20 C++ Programming: From Problem Analysis to Program Design, Second Edition20 If client wants integer dynarray class GList { public: void Insert( /* in */ ItemType item ); void Delete( /* in */ ItemType item ); bool IsPresent( /* in */ ItemType item ) const; private: int length; ItemType data[MAX_LENGTH]; }; int

21 C++ Programming: From Problem Analysis to Program Design, Second Edition21 Template Member Function template void GList ::Insert( /* in */ ItemType item ) { data[length] = item; length++; } void GList ::Insert( /* in */ int item ) { data[length] = item; length++; }

22 C++ Programming: From Problem Analysis to Program Design, Second Edition22 Standard Template Library STL is a collection of template classes for common data structures STL is available for C++ compilers on most platforms Using STL can save you time implementing code that uses common data structures More later

23 C++ Programming: From Problem Analysis to Program Design, Second Edition23 Summary Templates: A method to create multiple functions from one template function when they only differ in a data type.


Download ppt "C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered."

Similar presentations


Ads by Google