Notes from C++ Introduction

A simple data structure in C and in C++:

We begin with a comparison between a stack implemented in both C and in C++. Note how similar they are! Here is a comparison of how we would use these stacks in a program:


The member functions,or methods, of a class do not have to be defined inside the class. In fact, they are usually in a different file. Here is the stack example with the functions declared inside the class, but defined outside: The order of the class's member data and member functions does not matter. Also, public members are accessible outside the class, while private members can only access by the classes member functions. Until you start using class inheritance, you can consider members marked "protected:" to be the same as private.

Dynamic Memory Allocation:

C++ replaces malloc() and free() with new and delete. Here is a comparison of allocating and deallocating memory in C and in C++:


This last slide demonstrates two points. First, using a pointer to a class in C++ is the same as using a pointer to a struct in C. Second, you don't have to declare your variables at the top of your function. In fact, you can declare a loop counter inside the for loop:

Last modified Mon Sep 25 1997 by jslevene