UMBC CMSC202, Computer Science II, Fall 1998,
Sections 0101, 0102, 0103, 0104
5. More C++ Declarations & Definitions
Tuesday September 15, 1998
Assigned Reading: 3.6-3.9
Handouts (available on-line): none
Programs from this lecture.
- Recap the 4 kinds of scope (local, function, file, class) and 3
kinds of allocation methods (automatic, static, dynamic) mentioned
in the previous lecture.
- We consider an embellishment of the Widget class using the
following class declaration.
Some things to note:
- The constructor has an optional parameter. This allows us
to use the same function as the default constructor and as the constructor
with a size parameter. (See implementation file widget2.C.)
- This class uses two static data members. The count
member keeps track of the number of Widgets that are in existence.
The sequence member keeps track of the number of Widgets
that were ever constructed. These numbers might be different because
a Widget defined as a local variable in a function is destroyed when
that function returns. In order to keep the value of count
correct, we must have a destructor member function in the
Widget class. The name of the destructor starts with ~ and
is followed by the name of the class. So, the destructor of the
Widget class is ~Widget().
- The main program using the
Widget class shows how the count and sequence static
data members change in value. See
- In the next program, we examine the
addresses of dynamically allocated arrays. Since memory for these arrays are
allocated in the "heap" we notice that the addresses of these arrays
are very different from the addresses of the local variables or the
statically allocated global variables.
In this example, the addresses for the array that A and
B points to are very close. See sample run. However, there is a block
of memory 16 bytes long that is between these two arrays. If we
accidentally overrun the array A with assignments, we can end up
overwriting the memory for B and those mysterious 16 bytes.
It turns out that those 16 bytes were used by the memory manager
to keep track of the sizes of the blocks of memory that were allocated.
Thus, when we delete the array A and the array B, this messes up the
internal tables used by the memory manager. The result is a
segmentation fault the next time we try to allocate memory.
- The previous example shows that C/C++ is happy to let the programmer
use parts of the memory in unintended ways. This is also a problem with
classes in C++. While it is true that we should not access a
private data member of a class other than from a member function, this does
not mean that we cannot do so. In this program, we use an integer pointer to
change the size member of a Widget from the main function.
(See sample run.)
Recall that the size member is a private member in the Widget
class declaration. (See header file.)
Nevertheless by type casting a pointer to Widget into a pointer to
int we can gain access to both the size and
serial members of an object. Thus, you should not think that C++
provides secure protection for private data members.
- Java is a language that does not allow the programmer to manipulate
pointers directly. Thus, Java provides a more secure environment for
29 Oct 1998 17:14:04 EST
to Fall 1998 CMSC 202 Section Homepage