Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.

Similar presentations


Presentation on theme: "CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for."— Presentation transcript:

1 CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for a linked list First example will refine a program several times –Ending up with a specialized reusable function template –Shows how to write general and specialized templates Second example will walk through finished code –Look at how to declare and define a class template –Look at another data structure that uses dynamic memory

2 CSE 332: C++ template examples First Example: Printing Different Types #include using namespace std; int main (int, char *[]) { int i = 7; bool b = false; int * ip = &i; char * cp = "hello, world!"; void * vp = cp; cout << "i is " << i << endl; cout << "b is " << b << endl; cout << "ip is " << ip << endl; cout << "cp is " << cp << endl; cout << "vp is " << vp << endl; return 0; } Large number of types in C++ Each handled a bit differently For example, << with ostream Simply giving to cout gives i is 7 b is 0 ip is 0xfef69094 cp is hello, world! vp is 0x8048a3c Let’s make some improvements

3 CSE 332: C++ template examples Improve the Output for bool #include using namespace std; int main (int, char *[]) {... cout << "i is " << i << endl; cout << "b is " << boolalpha << b << endl; cout << "ip is " << ip << endl; cout << "cp is " << cp << endl; cout << "vp is " << vp << endl; return 0; } First, fix how bool is output Use an iostream manipulator std::boolalpha –prints true or false –Instead of 1 or 0 Now we get i is 7 b is false ip is 0xfef2aa04 cp is hello, world! vp is 0x8048b0c But what about the pointers?

4 CSE 332: C++ template examples Improve the Output for Pointers #include using namespace std; int main (int, char *[]) {... cout << "i is " << i << endl; cout << "b is " << boolalpha << b << endl; cout << "ip is " << ip << " (points to " << *ip << ")" << endl; cout (cp) << " (points to \"" << cp << "\")" << endl; cout << "vp is " << vp << endl; return 0; } Now, fix pointers Use reinterpret_cast –Convert char * to void * Use dereferencing –Convert int * to int Now we have what we want i is 7 b is false ip is 0xfef23584 (points to 7) cp is 0x8048b70 (points to "hello, world!") vp is 0x8048b70 But, don’t want to have to do all that over again –Next time we want to print

5 CSE 332: C++ template examples Refactor the Code with a Function Template #include using namespace std; #include "common_T.h" int main (int, char *[]) { int i = 7; bool b = false; int * ip = &i; char * cp = "hello, world!"; void * vp = cp; print(cout, "i is ", i); print(cout, "b is ", b); print(cout, "ip is ", ip); print(cout, "cp is ", cp); print(cout, "vp is ", vp); return 0; } Define print function template –Consistent interface across types –Just pass message, variable Without specialization, we get i is 7 b is 0 ip is 0xfeea28f4 cp is hello, world! vp is 0x8048adc Right back where we started? template void print (ostream & os, const char * message, const T & t) { os << message << t << endl; }

6 CSE 332: C++ template examples Specializations Give Polymorphism template <> void print (ostream & os, const char * message, const bool & b) { os << message << std::boolalpha << b << endl; } template <> void print (ostream & os, const char * message, const charptr & s) { os << message (s); if (s != 0) { os << " (points to \"" << s << "\")"; } os << endl; } template <> void print (ostream & os, const char * message, const intptr & ip) { os << message << ip; if (ip != 0) { os << " (points to " << *ip << ")"; } os << endl; } Specialize on individual types bool char * int * –Notice the use of typedef With specialization, we get i is 7 b is false ip is 0xfeebf064 (points to 7) cp is 0x8048c30 (points to "hello, world!") vp is 0x8048c30 And, we can reuse the solution! template void print (ostream & os, const char * message, const T & t) { os << message << t << endl; }

7 CSE 332: C++ template examples Second Example: Simple List Class Template Look at the header and source files in detail –I’ll provide these files on the web for you to study, too –But we’ll talk about them in detail today Member function template declarations & definitions Nested class inside a class template One (fortunately rare) issue you should know about –What can you do when the compiler crashes???

8 CSE 332: C++ template examples For Next Time C++ STL Containers Assigned Reading –pages 877-889 (STL overview, vectors) pages 905-922 (container types and concepts) pages 1095-1102 (containers’ functions)


Download ppt "CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for."

Similar presentations


Ads by Google