COMP151 Lab 12
template & operator overloading

Objective:

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
 
::
.*
.
?:
sizeof

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:

To complete evector.cpp, you need to:
  1. Fill in your name, student id and ITSC/CSD account id at the beginning of the evector.cpp as comment
  2. 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.
  3. 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.
  4. 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.
  5. Remove the "/*" and "*/" in the main program
  6. compile the program named as: evector
  7. If your program works properly, it should give the following output

Hints:

Submission:


This page is created by Chan Ka Ki.