EECE3326_Jan9_2014 - Template, Template Classes, Standard Template Library (STL), Vector Class

3 Pages
Unlock Document

Northeastern University
Electrical and Comp Engineering
EECE 2560
Waleed Meleis

Introduction to C++ 1/9 Templates //Used when you might want to have different data types as inputs, e.g. strings, floats, ints. Templates are similar to overloading void writeArray(const int arr{}, int n) { for(int i =0; i < n; i++) cout < // template only applies to the next function. void writeArray(const T arr[], int n) { for (int i = 0; i T add (T x, T y) { return x+y; } cout << add(10,1.5); // compiler will give an error; tells us math only wants to use same data types for math cout << add (10,1.5); //explicit instantiation —> explicitly telling the compiler which version of the function we want to use Template Classes //template call tells us that the entire following class uses data type T…the template class has template functions template class Circle { public: Circle (const T &item); T f(); void g (const T &item); private: T dataValue; }; template class store { public: store (const T &item = T() ) //&item is a reference to a const value of type T… when we set “&item = T()”, we’re setting a default value for the T class T getValue() const; void setValue (const T &item); private: T value; }; store intStore(5); // creating a class object called intStore, which is a “store of int” —> passing int in for T // explicit instantiation store charStore(“x”); store
More Less

Related notes for EECE 2560

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.