GenericsTemplates

=Generics/Templates=

This type of implementation is a type or method to operate on objects of various types while providing compile-time type safety. In Java it is called generics and Templates in C++. They both have very similar implementations.

In Java
Generics in Java are created with a less than sign(<) followed by what you want to name your type and a greater than sign(>). You could have more than one type by inserting commas between them. They are a cool way to have an information value of any type in your class. Such as in this example: code format="java" public class SomeClass{ T myObject; //generic type object named myObject } code

They are most often seen in Java when you create a list or map of any kind. code format="java" List myList; //specify the generic type to the String class myList = new ArrayList; //call the constructor myList.add("Derp"); //the add method now accepts only strings

Map myMap; //the map interface accepts two generic types myMap = new HashMap; myMap.put(555, "Derp"); //the put method now only accepts an int followed by a string code

If you haven't figured out from the example above, generics can also be implemented into interfaces. code format="java" public interface List{ void add(T t); T get(int index); Iterator iterator; //returns a iterator with the generic set to the specified type } public interface Iterator{ E next; boolean hasNext; }

public interface Map{ void put(X x, Y y); Y get(X x); } code

Java also lets you specify wildcards to specify the values accepted into your generic class to a specific child. Consider the following problem: code format="java" List ints = new ArrayList; ints.add(2);

List nums = ints; //valid if List were a subtype of List according to substitution rule. nums.add(3.14);

Integer x=ints.get(1); // now 3.14 is assigned to an Integer variable! code

This can be fixed by assigning a wildcard like so: code format="java" List ints = new ArrayList; ints.add(2);

List<? extends Number> nums; //wildcard '?' nums = ints; // it is OK nums.add(3.14); // It is prohibited code

Generics can also be used with individual methods. This function is used for a map entry. It returns Entry<T,T> and the generic type is specified with <T>. It returns an entry with value set to the key and value. code format="java" public static <T> Entry<T,T> twice(T value){ return new Entry<T,T>(value, value); } code

More information on generics [|here.]

In C++
In C++ there are two types of templates; function templates and class templates. A function template behaves like a function except the template can be arguments of many different types. So instead of creating the same function for every type you plan to use, you just have to create one. The format for declaring function templates is: code format="cpp" template <typename someName> returnType functionName(funct values){ body } code

The common implementation is number comparison. This function will work with any type as long as the (>)operator is declared for it. code format="cpp" using namespace std;
 * 1) include

template <typename T> T max(T x, T y){ if(a > b) return a;  else return b; }

int main{ cout<< max(3, 7) <<endl; cout<< max(5.55, 8.77) <<endl;

return 0; } code

A class template is similar to java generics. code format="cpp" template <class T> class Stack{ public: Stack(int); ~Stack; bool push(T*); T *pop; bool isEmpty; bool isFull; private: int size; int top; T *stackPtr; } code

Get more information on C++ templates[| here.]