Object-Oriented Programming (OOP) Lecture No. 37

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
C/c++ 4 Yeting Ge.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Intro to Generic Programming Templates and Vectors.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
ECE122 Feb. 22, Any question on Vehicle sample code?
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
1 CSC241: Object Oriented Programming Lecture No 25.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
1 ENERGY 211 / CME 211 Lecture 18 November 2, 2007.
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Motivation for Generic Programming in C++
CS212: Object Oriented Analysis and Design
C++ Template.
Introducing Templates and Generics in C++
How to be generic Lecture 10
C++ Templates.
Templates.
Object-Oriented Programming (OOP) Lecture No. 45
Exceptions, Templates, and the Standard Template Library (STL)
Function Overloading.
OOP-4-Templates, ListType
Constructor & Destructor
Programmazione I a.a. 2017/2018.
Generic Programming Techniques in C++
Templates.
Object-Oriented Programming (OOP) Lecture No. 32
Object-Oriented Programming (OOP) Lecture No. 36
Object-Oriented Programming (OOP) Lecture No. 39
Name: Rubaisha Rajpoot
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Exceptions and Templates
Object-Oriented Programming (OOP) Lecture No. 40
Local Variables, Global Variables and Variable Scope
Exception Handling.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Standard Version of Starting Out with C++, 4th Edition
Object-Oriented Programming (OOP) Lecture No. 22
Review for Final Exam.
Exceptions, Templates, and the Standard Template Library (STL)
Object-Oriented Programming (OOP) Lecture No. 38
Object-Oriented Programming (OOP) Lecture No. 33
Object oriented programming (OOP) Lecture No. 7
C Programming Lecture-8 Pointers and Memory Management
Templates I CMSC 202.
Template.
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Generic Programming.
Object-Oriented Programming (OOP) Lecture No. 34
ENERGY 211 / CME 211 Lecture 23 November 12, 2008.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 37

Resolution Order template< typename T > class Vector { … }; class Vector< T* > { … }; template< > class Vector< char* > { … };

Resolution Order Compiler searches a complete specialization whose type matches exactly with that of declaration If it fails then it searches for some partial specialization In the end it searches for some general template

…Example – Resolution Order int main() { Vector< char* > strVector; // Vector< char* > instantiated Vector< int* > iPtrVector; // Vector< T* > instantiated Vector< int > intVector; // Vector< T > instantiated return 0; }

Function Template Overloading template< typename T > void sort( T ); void sort( Vector< T > & ); template< > void sort< Vector<char*> >( Vector< char* > & ); void sort( char* );

Resolution Order Compiler searches target of a function call in the following order Ordinary Function Complete Specialization Partial Specialization Generic Template

Example – Resolution Order int main() { char* str = “Hello World!”; sort(str); // sort( char* ) Vector<char*> v1 = {“ab”, “cd”, … }; sort(v1); //sort( Vector<char*> & )

…Example – Resolution Order Vector<int> v2 = { 5, 10, 15, 20 }; sort(v2); // sort( Vector<T> &) int iArray[] = { 5, 2, 6 , 70 }; sort(iArray); // sort( T ) return 0; }

Templates and Inheritance We can use inheritance comfortably with templates or their specializations But we must follow one rule: “Derived class must take at least as many template parameters as the base class requires for an instantiation”

Derivations of a Template A class template may inherit from another class template template< class T > class A { … }; class B : public A< T >

…Derivations of a Template int main() { A< int > obj1; B< int > obj2; return 0; }

…Derivations of a Template A partial specialization may inherit from a class template template< class T > class B< T* > : public A< T > { … };

…Derivations of a Template int main() { A< int > obj1; B< int* > obj2; return 0; }

…Derivations of a Template Complete specialization or ordinary class cannot inherit from a class template template< > class B< char* > : public A< T > { … }; // Error: ‘T’ undefined class B : public A< T >

Derivations of a Partial Sp. A class template may inherit from a partial specialization template< class T > class A { … }; class A< T* >

…Derivations of a Partial Sp. template< class T > class B : public A< T* > { … } int main() { A< int* > obj1; B< int > obj2; return 0; }

…Derivations of a Partial Sp. A partial specialization may inherit from a partial specialization template< class T > class B< T* > : public A< T* > { … };

…Derivations of a Partial Sp. int main() { A< int* > obj1; B< int* > obj2; return 0; }

…Derivations of a Partial Sp. Complete specialization or ordinary class cannot inherit from a partial specialization template< > class B< int* > : public A< T* > { … } // Error: Undefined ‘T’ class B : public A< T* >

Derivations of a Complete Sp. A class template may inherit from a complete specialization template< class T > class A { … }; template< > class A< float* >

…Derivations of a Complete Sp. template< class T > class B : public A< float* > { … }; int main() { A< float* > obj1; B< int > obj2; return 0; }

…Derivations of a Complete Sp. A partial specialization may inherit from a complete specialization template< class T > class B< T* > : public A< float* > { … };

…Derivations of a Complete Sp. int main() { A< float* > obj1; B< int* > obj2; return 0; }

… Derivations of a Complete Sp. A complete specialization may inherit from a complete specialization template< > class B< double* > : public A< float* > { … };

… Derivations of a Complete Sp. int main() { A< float* > obj1; B< double* > obj2; return 0; }

… Derivations of a Complete Sp. An ordinary class may inherit from a complete specialization class B : public A< float* > { … };

… Derivations of a Complete Sp. int main() { A< float* > obj1; B obj2; return 0; }

Derivations of Ordinary Class A class template may inherit from an ordinary class class A { … }; template< class T > class B : public A

…Derivations of Ordinary Class int main() { A obj1; B< int > obj2; return 0; }

…Derivations of Ordinary Class A partial specialization may inherit from an ordinary class template< class T > class B< T* > : public A { … };

…Derivations of Ordinary Class int main() { A obj1; B< int* > obj2; return 0; }

…Derivations of Ordinary Class A complete specialization may inherit from an ordinary class template< > class B< char* > : public A { … };

…Derivations of Ordinary Class int main() { A obj1; B< char* > obj2; return 0; }