COMP151 Lab 12
template & operator overloading
Objective:
-
To learn template and operator overloading in C++
Files:
Lab work:
What is a template?
Templates can generate particular instances of a class/function varying
by type. We parameterize all or some types in a function/class.
template syntax
// template function
template<typename T> T min(const T& t1, const T& t2) {
return t1 < t2? t1: t2;
};
// template class
template<typename T> class Array {
public:
// ...
T& at(int i);
};
// instantiate template function implicitly
int i = min(10, 20);
// instantiate template function explicitly
double d = min<double>(4, 5.5);
// instantiate template class
Array<int> ai;
//...
Template example
In templatefunc.cpp, min is a template
function. Try running it to see its output. Note the use of
min<double>(5, 7.50) instead of just min(5, 7.50).
Why do explicitly instantiate the function template with a <double>.?What
would have happened if we had written min(5, 7.50)?
Now remove the "/*" and "*/" in the main program,
and try to recompile the program. The program will not compile because
min is called with 2 Dummy objects as parameters.
What is the problem? How can you fix it?
What is operator overloading?
Customization of C++ operators. You can overload most of the C++
operators. But you CANNOT invent your own operator!
The following are nonoverloadable operators
Operator overloading syntax
// global function
ostream& operator<<(ostream& os, const Dummy& dummy) {
// ...
};
For an example of overloading a non-global operator, see the overloaded operator += of evector in evector.cpp.
Assessment:
-
evector is a template class that is publically derived from the
template class vector in STL. It inherits all of the vector member
functions including the operator[] from vector so that you
can access each individual element by [], e.g. ev[5] returns the 6th element.
-
+= is an overloaded member function. It adds the second
parameter to each element in the evector
-
<< is an overloaded global function. It prints all of the
elements in the evector separated by spaces.
To complete evector.cpp, you need to:
-
Fill in your name, student id and ITSC/CSD account id at the beginning
of the evector.cpp as comment
-
Overload the "-=" operator. This is similar to "+=",
except that it subtracts the second parameter from each element in
the evector. You may assume that operator"-" is defined
on objects of class T.
-
Overload "*" operator to do "dot product" of 2 evectors. If the
sizes of 2 evectors are the same, it should create a new evector
such that the new i-th element is equal to the product of the i-th elements
of the 2 evectors. If the sizes of the 2 evectors are different, it
returns an empty evector. Note that this is a member function You
may assume that operator "*" is defined on objects of class T.
-
Overload the "+" operator to do addition of 2 evectors. If the sizes
of the 2 evectors are the same, it should create a new evector
such that the new-ith element is equal to the sum of the i-th elements
of 2 evectors. If the sizes of the 2 evectors are different, it returns
an empty evector. Note that this is a member function. You may assume that
operator "+" is defined on objects of class T.
-
Remove the "/*" and "*/" in the main program
-
compile the program named as: evector
-
If your program works properly, it should give the following output
Hints:
-
the function prototype to overload * is "evector<T> evector<T>::operator*(const
evector<T>&)"
-
the function prototype to overload + is "evector<T> evector<T>::operator+(const
evector<T>&)"
Submission:
-
Put your file into ~/comp151/lab12
-
The system will collect your files at mid-night of the lab day
This page is created by Chan Ka Ki.