Assigned Reading: 1.1 - 1.16
Handouts (available on-line):
Topics Covered:
We mentioned the first of many scope rules, saying that C is lexically scoped, i.e. an identifier is know only from the point in code where it first appears.
We also discussed the assert macro, and how it works. A sample file and its output show that when an assertion is true (i.e. its argument evaluates to non-zero), the assert succeeds, and there are no other consequences than evaluating the assert statement's argument. If the assertion fails, however, the result is that program execution halts, a core dump is taken, and a message is written to the standard error device, which for C++ is known as cerr
Classes have the structure
A class is an object-oriented (indeed, a very beefed-up) extension of the C struct, including both data members and member functions. We examined a simple Header file for the Account class, and also its implementation. We noted that a class allows its members to be hidden from all outside access by placing them in a private section, and that by placing members in a public section, they may be accessed from outside the class.
We noted that a member function of a class has access to all members of the class, whether they are public or private.
We talked briefly about class constructors. A class constructor is a member function of a class which:
A default class constructor is one with no arguments. If you don't provide any constructors for your class, then C++ will generate a default constructor for you (it is a very bad idea not to write your own constructor!).
You may have several constructors for a class, differing only in their paramater list. The technique of reusing a function name by writing several versions, each with different parameter lists is called overloading a function name. C++ allows us to overload virtually all function names that we write. If you write an constructor of any kind at all, then C++ won't generate a default constructor for you. Be warned. The vector class shows an example of overloading a class constructor. The class implementation shows how to code this.
We discussed the scope resolution operator, ::. The scope resolution
operator is needed to associate a function that is defined outside of a
class with the class. For example, the function
void foo(int);is not associated with any class at all; if we want to write the implementation for some member function named foo (note: outside the class declaration), we'd write something like
void fooclass::foo(int x);
Finally, we noted that it is possible to overload operators in a more-or-less natural way.