Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Generics and the ArrayList Class
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 23 : Generics King Fahd University of Petroleum & Minerals College of Computer Science.
Generics and The ArrayList Class
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
 2006 Pearson Education, Inc. All rights reserved Templates.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Stacks and Queues. Sample PMT online… Browse 1120/sumII05/PMT/2004_1/
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
Introduction to Generics
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Copyright 2006 Pearson Addison-Wesley, 2008, 2009 Joey Paquet 9-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Classes, Interfaces and Packages
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
Java Generics.
Java Primer 1: Types, Classes and Operators
Java Generics.
Comp 249 Programming Methodology
Comp 249 Programming Methodology
Comp 249 Programming Methodology
CMSC 202 Generics.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Comp 249 Programming Methodology
CMSC 202 Generics.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
CMSC 202 Exceptions.
Presentation transcript:

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 – Advanced Programming Practices Generic Programming

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 2 Learning Objectives  Introduction: Function Templates in C++  Java Generics

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 3 Introduction  C++ templates  Allow very "general" definitions for functions and classes  Type names are "parameters" instead of actual types  Precise definition determined at run-time

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 4 Function Templates  Typical function swapValues: void swapValues(int& var1, int& var2) { int temp; temp = var1; var1 = var2; var2 = temp; }  Applies only to variables of type int  But code would work for any types!

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 5 Function Templates vs. Overloading  Could overload function for char’s: void swapValues(char& var1, char& var2) { char temp; temp = var1; var1 = var2; var2 = temp; }  But notice: code is nearly identical!  Only difference is type used in 3 places

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 6 Function Template Syntax  Allow "swap values" of any type variables: template void swapValues(T& var1, T& var2) { T temp; temp = var1; var1 = var2; var2 = temp; }  First line called "template prefix"  Tells compiler what’s coming is "template"  And that T is a type parameter

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 7 Template Prefix  Recall: template  In this usage, "class" means "type", or "classification"  Can be confused with other "known" use of word "class"!  C++ allows keyword typename in place of keyword class here  But most use class anyway

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 8 Template Prefix  Again: template  T can be replaced by any type  Predefined or user-defined (like a C++ class type)  In function definition body:  T used like any other type  Note: can use other than T, but T is "traditional" usage

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 9 Function Template Definition  swapValues() function template is actually large "collection" of definitions!  A definition for each possible type!  Compiler only generates definitions when required  But it’s "as if" you’d defined for all types  Write one definition  works for all types that might be needed

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 10 Calling a Function Template  Consider following call: swapValues(int1, int2);  C++ compiler "generates" function definition for two int parameters using template  Likewise for all other types  Needn’t do anything "special" in call  Required definition automatically generated

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 11 //Program to demonstrate a function template. #include using std::cout; using std::endl; //Interchanges the values of variable1 and variable2. //The assignment operator must work for the type T. template void swapValues(T& variable1, T& variable2) { T temp; temp = variable1; variable1 = variable2; variable2 = temp; } Calling a Function Template: Example

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 12 int main( ) { int integer1 = 1, integer2 = 2; cout << "Original integer values are " << integer1 << " " << integer2 << endl; swapValues(integer1, integer2); cout << "Swapped integer values are " << integer1 << " " << integer2 << endl; char symbol1 = 'A', symbol2 = 'B'; cout << "Original character values are: " << symbol1 << " " << symbol2 << endl; swapValues(symbol1, symbol2); cout << "Swapped character values are: " << symbol1 << " " << symbol2 << endl; return 0; }

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 13 Java Generics  In Java, templates are called “Generics”.  C++ uses function templates and class templates. As Java does not have free functions, it only has “generic classes”.  C++ uses a totally different approach for the “instantiation” of generic classes, compared to Java.  The C++ compiler generates code for each occurrence of the use of a template, then compiles this code. Executed code is not generic.  Java compiles generic classes and uses as generic during execution.

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 14 Java : A Class Definition with a Type Parameter

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 15 Class Definition with a Type Parameter  A class that is defined with a parameter for a type is called a generic class or a parameterized class  The type parameter is included in angular brackets after the class name in the class definition heading  Any non-keyword identifier can be used for the type parameter, but by convention, the parameter starts with an uppercase letter  The type parameter can be used like other types used in the definition of a class

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 16 Tip: Compile with the -Xlint Option  There are many pitfalls that can be encountered when using type parameters  Compiling with the -Xlint option will provide more informative diagnostics of any problems or potential problems in the code javac –Xlint Sample.java

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 17 A Generic Ordered Pair Class (Part 1 of 4)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 18 A Generic Ordered Pair Class (Part 2 of 4)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 19 A Generic Ordered Pair Class (Part 3 of 4)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 20 A Generic Ordered Pair Class (Part 4 of 4)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 21 Using Our Ordered Pair Class (Part 1 of 3)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 22 Using Our Ordered Pair Class (Part 2 of 3)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 23 Using Our Ordered Pair Class (Part 3 of 3)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 24 Pitfall: A Generic Constructor Name Has No Type Parameter  Although the class name in a parameterized class definition has a type parameter attached, the type parameter is not used in the heading of the constructor definition public Pair ()  A constructor can use the type parameter as the type for a parameter of the constructor, but in this case, the angular brackets are not used public Pair(T first, T second)  However, when a generic class is instantiated, the angular brackets are used Pair pair = new Pair ("Happy", "Day");

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 25 Pitfall: A Primitive Type Cannot be Plugged in for a Type Parameter  The type plugged in for a type parameter must always be a reference type  It cannot be a primitive type such as int, double, or char  However, now that Java has automatic boxing, this is not a big restriction  Note: reference types can include arrays

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 26 Pitfall: A Type Parameter Cannot Be Used Everywhere a Type Name Can Be Used  Within the definition of a parameterized class definition, there are places where an ordinary class name would be allowed, but a type parameter is not allowed  In particular, the type parameter cannot be used in simple expressions using new to create a new object  For instance, the type parameter cannot be used as a constructor name or like a constructor: T object = new T(); T[] a = new T[10];

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 27 Pitfall: An Instantiation of a Generic Class Cannot be an Array Base Type  Arrays such as the following are illegal: Pair [] a = new Pair [10];  Although this is a reasonable thing to want to do, it is not allowed given the way that Java implements generic classes

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 28 Using Our Ordered Pair Class and Automatic Boxing (Part 1 of 3)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 29 Using Our Ordered Pair Class and Automatic Boxing (Part 2 of 3)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 30 Using Our Ordered Pair Class and Automatic Boxing (Part 3 of 3)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 31 Pitfall: A Class Definition Can Have More Than One Type Parameter  A generic class definition can have any number of type parameters  Multiple type parameters are listed in angular brackets just as in the single type parameter case, but are separated by commas

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 32 Multiple Type Parameters (Part 1 of 4)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 33 Multiple Type Parameters (Part 2 of 4)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 34 Multiple Type Parameters (Part 3 of 4)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 35 Multiple Type Parameters (Part 4 of 4)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 36 Pitfall: A Generic Class Cannot Be an Exception Class  It is not permitted to create a generic class with Exception, Error, Throwable, or any descendent class of Throwable  A generic class cannot be created whose objects are throwable public class GEx extends Exception  The above example will generate a compiler error message

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 37 Using a Generic Class with Two Type Parameters (Part 1 of 2)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 38 Using a Generic Class with Two Type Parameters (Part 2 of 2)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 39 Bounds for Type Parameters  Sometimes it makes sense to restrict the possible types that can be plugged in for a type parameter T  For instance, to ensure that only classes that implement the Comparable interface are plugged in for T, define a class as follows: public class RClass  " extends Comparable " serves as a bound on the type parameter T  Any attempt to plug in a type for T which does not implement the Comparable interface will result in a compiler error message

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 40 Bounds for Type Parameters  A bound on a type may be a class name (rather than an interface name)  Then only descendent classes of the bounding class may be plugged in for the type parameters public class ExClass  A bounds expression may contain multiple interfaces and up to one class  If there is more than one type parameter, the syntax is as follows: public class Two

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 41 A Bounded Type Parameter

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 42 Tip: Generic Interfaces  An interface can have one or more type parameters  The details and notation are the same as they are for classes with type parameters

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 43 Generic Methods  When a generic class is defined, the type parameter can be used in the definitions of the methods for that generic class  In addition, a generic method can be defined that has its own type parameter that is not the type parameter of any class  A generic method can be a member of an ordinary class or a member of a generic class that has some other type parameter  The type parameter of a generic method is local to that method, not to the class

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 44 Generic Methods  The type parameter must be placed (in angular brackets) after all the modifiers, and before the returned type public static T genMethod(T[] a)  When one of these generic methods is invoked, the method name is prefaced with the type to be plugged in, enclosed in angular brackets String s = NonG. genMethod(c);

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 45 Inheritance with Generic Classes  A generic class can be defined as a derived class of an ordinary class or of another generic class  As in ordinary classes, an object of the subclass type would also be of the superclass type  Given two classes: A and B, and given G: a generic class, there is no relationship between G and G  This is true regardless of the relationship between class A and B, e.g., if class B is a subclass of class A

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 46 A Derived Generic Class (Part 1 of 2)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 47 A Derived Generic Class (Part 2 of 2)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 48 Using UnorderedPair (Part 1 of 2)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 49 Using UnorderedPair (Part 2 of 2)

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 50 Principle: Algorithm Abstraction  Refers to implementing templates  Express algorithms in "general" way:  Algorithm applies to variables of any type  Ignore incidental detail  Concentrate on substantive parts of algorithm  Assumes that the same algorithm, using the same operators, can be used across different types.  Major restriction in designing generic code  Function templates are one way C++/Java support algorithm abstraction.

Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 51 Defining Templates: Strategy  Develop function normally  Using actual data types  Completely debug "ordinary" function  Then convert to template  Replace type names with type parameter as needed  Advantages:  Easier to solve "concrete" case  Deal with algorithm, not template syntax