Section notes for Week 2 (More C++)

Section Notes for Week 2 (More C++)

Story thus far

  1. Conversion of Java into C++
  2. C++ I/O
  3. C++ Preprocessor
  4. Pointers, local variables, and the stack
  5. new and delete
  6. Code Review
  7. More Example Code



  1. Reference Variables and Method invocation
  2. Const keyword
  3. Constructors, copy constructors, and destructors
  4. Operator Overloading and the string class
  5. Big 3
  6. Templates and the array class

Reference Variables

So as Jason discussed in class, there are a 3 ways to handle passing information to and returning information from functions: by value, by pointer, and by reference.

Hopefully pointers and value passing shouldn't be a big surprise after last week. But this reference idea is new. Just like in Java, a reference in C++ is a wussy pointer. You can either think of it as such, or just as an "alias" for another variable. Try not to get confused with the use of & in the variable declaration. Notice that in declaration, both * and & have different meanings than their use in code.

  • Lets see some of these bad boys in action.

    The Const Keyword

    So we have yet more keywords and noise to pay attention to. This const keyword means that whatever it applies to is non-modifiable. The trick is telling what it applies to.

    As I may have mumbled last week, declarations are always read backwards, with each keyword/modifier applying to the item immediately preceding it.

  • Example: Lets play with moving this guy around, using it in function definitions, return types, pointers, etc.

    Constructors, Copy Constructor, and Destructors.

    Unlike Java, C++ does not always provide you with default constructors. According to the C++ standard, getting one depends on if you have a specific constructor or not.

  • Example: Default parameters
  • Example: Pitfalls when declaring objects (Class var() is bad).
  • Example: Arrays of objects

    So user-defined constructors work just like they did in Java. Copy Constructors, however, are new. They are used when you pass or return an object by value, or when you explicitly create a new object by passing in an old object to the constructor.

    C++ does provide you with default copy constructors (and destructors), but when you have pointers involved, they don't work quite right. Some handwaving on the board will demonstrate why.

    So copy constructors need to take a non-pointer object as a parameter, yet they are invoked upon pass-by-value. This can lead to a catch-22.. How do you declare the prototype for the copy constructor to avoid triggering the copy constructor itself?

  • Example: Copy constructors

    Destructors are called for objects on the stack when the function call returns, or when delete is called for objects on the heap. They are also called for objects contained inside a class. They are NOT called when pointers or references to objects leave scope, or for pointers inside a an object that has been deleted.

  • Example: Destructors getting called upon leaving scope and on delete

    Example Code: stringuse.C, string.C, string.h.

    Operator Overloading

    So, operator overloading. Operator overloading allows us to redefine the existing operators to do special things in our particular class.

    Operator overloading is done by defining a special kind of method function in the class we wish to overload an operator for. Lets spend a brief minute thinking about how operators work normally, and then work from there up to how this function prototype for these things should work.

  • Example on board with operators + and =

    Why would you want to overload =?

  • Example in code with +, =, [], < in string class (linked above)

    The Law of the Big 3

    WAKE UP! This is key.

    "If you need to write either a copy constructor, a destructor, or an operator=, you need to write all three"

    Why is this? Can anyone think of any exceptions?

    The easiest one (for me at least) to overlook was always operator=.


    Templates are cool. They allow you to define a class or function to take a type as a parameter, and are useful for building what are called abstract data types. For example, lets have a look at the array class you will be using in your MP's.

  • Example of the template array class. Can you predict which operators, constructors and destructors will be called, and how many of each?

    Example Code: arrayuse.C, array.C, array.h.