Lecture 2
Learning objectives
After this class (which reviews C++ material that you should already know!), you should be able to:
- Describe the concept of an abstract data type, and how C++ classes help implement them.
- Mention two benefits of data hiding.
- Mention which type of file typically contains the interface, and which the implementation, in C++.
- Explain why templates are normally defined in .h files.
- Define and use template functions and classes.
- Give examples of some common errors with the use of pointers.
- Given code with incorrect use of pointers, identify the problems and correct them.
- Explain when "deep copy" is needed, why it is needed, and what are the typical features that need to be implemented to ensure deep copy.
- Explain why self-assignment can lead to incorrect code, when we overload assignment to ensure deep copy.
- Explain how a friend function is different from others.
Reading assignment
- Chapter 1, up to sec 1.6 (except sec 1.3 and 1.5).
- Optional: Fall 2003 lecture notes has additional explanations for templates (chapter 5), and on deep copy (chapter 2).
Exercises and review questions
- Exercises and review questions on current lecture's material
- What are two benefits of information hiding?
- Will the interface normally be in a .h file or in a .cpp file?
- Will the implementation normally be in a .h file or in a .cpp file?
- Why are templates normally defined in .h files?
- Write a template function called
average
, which computes the average of an array, and give an example code demonstrating its use.
- Write a template class called
complex
, which stores a complex number. The components may either be int, float, or double. Include an operator for adding two complex numbers, and for outputting a complex number. (Note: A complex number is a pair of numbers (x,y). The sum of complex numbers c1 = (x1, y1) and c2 = (x2, y2) is defined as the complex number (x1+x2, y1+y2).)
- Chapter 1, exercise #10. Also, fix the error.
- Why was the operator
<<
made a friend in the Pair
class?
- Questions on next lecture's material
- Can you find out where the STL header files are, on the machine linprog?
Last modified: 30 Aug 2004