chap14 - Chapter 14 Chapter Templates and Polymorphism Generic functions and classes Polymorphic Functions What are they Generic functions that can act

chap14 - Chapter 14 Chapter Templates and Polymorphism...

This preview shows page 1 - 9 out of 34 pages.

Chapter 14 Chapter 14 Templates and Polymorphism Generic functions and classes
Image of page 1
Polymorphic Functions What are they? Generic functions that can act upon objects of different types The action taken depends upon the types of the objects Where have we seen them before? Function overloading Define functions or operators with the same name Rational addition operator + Function Min() for the various numeric types Primitive polymorphism
Image of page 2
Polymorphic Functions Templates Generate a function or class at compile time Where have we seen them before? Standard Template Library Vector and other container classes True polymorphism Choice of which function to execute is made during run time C++ uses virtual functions
Image of page 3
Function Overloading We rewrite functions Min(), Max(), and Sort() for many different types int Min( int a, int b) { if (a < b) return a; else return b; } double Min( double a, double b) { if (a < b) return a; else return b; } Rational Min(Rational a, Rationalb) { if (a < b) return a; else return b; } There has to be a better way!
Image of page 4
Function Templates Function template Describes a function format that when instantiated with particulars generates a function definition Write once, use multiple times template < class T> T Min( const T &a, const T &b) { if (a < b) return a; else return b; } Indicates a template is being defined T is a formal template type parameter
Image of page 5
Template Function Code segment int Input1 = PromptAndRead(); int Input2 = PromptAndRead(); cout << Min(Input1, Input2) << endl; Causes the following function to be generated from our template int Min( const int &a, const int &b) { if (a < b) return a; else return b; }
Image of page 6
Template Function Code segment double Value1 = 4.30; double Value2 = 19.54; cout << Min(Value1, Value2) << endl; Causes the following function to be generated from our template double Min( const double &a, const double &b) { if (a < b) return a; else return b; }
Image of page 7
Template Function Code segment Rational r(6,21); Rational s(11,29); cout << Min(r, s) << endl; Causes the following function to be generated from our template Rational Min( const Rational &a, const Rational &b) { if (a < b) return a; else return b; } Operator < needs to be defined for the actual template parameter type. If < is not defined, then a compile-time error occurs
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 34 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes