UMBC CMSC202, Computer Science II, Spring, 1999 Sections 0201 and 0202

Introduction

[Next Lecture]


Assigned Reading:  1.1 - 1.16

Handouts (available on-line):

Topics Covered:

Lectures 1 and 2, Feb 1 and 3, 1999

Lecture 1 covered administrivia. We went over course objectives, grading criteria, etc. All information on the class is available via its web page, and if you're reading this, you found that page.

Functions and Assertions
We reviewed the structure of a C function. A function has a header which includes the functions name, return type, and parameter list. A function's body is enclosed in a pair of French braces, { }.

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

Two simple classes
A class is the C++ mechanism for implementing encapsulation and information hiding.

Classes have the structure

class <u>class_name</u> { ..... } ; where class_name is any non-reserved identifier. Also, note the semicolon is required.

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:

From this discussion, we noted that the programmer never has to call a class constructor explicitly, and that it's an error to try to.

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.


Last Modified: 15 Feb 1999 13:04:37 EST by Mitch Edelman edelman@cs.umbc.edu

Back up to Spring 1999 CMSC 202 Section Homepage