Midterm Exam Programme
You need to be able to answer the following questions in detail.
Each question should evoke an example (usually, a few lines of code)
in your mind.
- Constructors, destructors, copy constructors.
- What is the difference between private and public
members of a class? What is the basic difference
between a C++ struct and a C++ class?
- What is a constructor?
- What is a default constructor? Give an example
when writing an explicit default constructor is necessary.
- What is a copy constructor? How does the compiler decide
which of the constructors described in a class (if any) is a
copy constructor (if any)?
Give an example
when writing an explicit copy constructor is necessary.
What happens if you don't write an explicit copy constructor?
- The same for operator= (also see overloading)
- What is the connection between copy constructors and
arguments passed to functions? What is the difference
between the operation of
void foo( X a );
void foo( X & a );
void foo( const X & a );
and what can you tell about what foo can do to an object
of type X passed to it as an argument (in particular, can
foo change it?)
- Why are objects of a type X so often passed to functions as
const X & (const reference to X) ?
- Write an example of code that passes an object
to a function by reference (so that the function can change the
value of the variable it is given as an argument)
using only (1) references & (2) pointers .
- What is a temporary object? How do you create cause a temporary
to be created? How long is it guaranteed
to exist?
- What is a destructor? When are destructors called? Give an
example of when you have to write a destructor explicitly.
- Can you create an object in a function and return a reference to it?
- Name the four methods (member functions) that are generated
by default for each class you declare. When can you hope
that those default methods do what you intend?
- What is the difference between member functions of a class
and friend functions of a class? What is the syntax for
declaring a function as a friend of a class?
- Can an instance of class (e.g. a variable of that type) find
what place in memory it occupies? Why knowing this may be useful?
- Overloading
- What is type checking? What role it plays in the work
of a C++ compiler?
- What is overloading? Why use it?
- Describe how the compiler chooses the right function to
compile into your code out of several functions with the
same name.
- What are overloaded operators? Give an example from a standard
library.
- Why and how does cout << x; know to print an integer x
differently than a double x, differently than a string x?
Is operator<< usually a friend or a member of a class?
Of which class can it be a member?
- Why binary operators are usually friends, while unary
operators are usually members?
- Write a class that represents time -- days, hours, minutes, seconds --
by keeping each of the above as separate data members, and
implements operator+ , operator<< etc.
- Should an overloaded operator+ return a new object or a
reference? Should an overloaded operator= return a new object or a
reference?
- What is a type conversion operator? How do you create one
for your own class?
- Dynamic memory allocation and container classes.
- Why use dynamic memory? Give an example when it is necessary.
- How do you request memory at run-time? How do you allocate
and initialize (1) an object (2) an array of objects ?
- What happens it you don't free up (how?) the memory allocated
with new[] ?
- Describe what problems can arise if a class that holds
pointers to dynamically allocated memory does not
have an explicit copy constructor and operator= .
- Explain the operation of a self-expanding table (see
my Table example).
- Implement your own string class that can handle strings
of arbitrary length (i.e. the only limitation is the size
of available memory) Concentrate on the copy constructor,
operator+ and operator= . (In particular, if a, b, c
are strings, c = b; b = a; a = c; should
swap the values of a and b .)