Advanced C++ Programming Concepts

The goal of this presentation is to get an understand of some of the more advanced C++ programming concepts.

New Data Types

This section will present some of the new data types available in C++. The example program dataType will demonstrate these.

bool

This data type allows use to use variables that may be "true" or "false".

long long

This data type allows us to specify double sized integers. On most machines this is 64 bits. MS does not allow this. They do have a LONGLONG data type.

long double

This data type allows use to specify doubly sized doubles. That is 128 bit floating point numbers. MS maps this into 10 bytes.

Name spaces

Associated with the standard C++ functions and objects are namespaces. Normally you must write std:: before things like cout. This is for all the new include files. Or you can write at the begining of a file

To define a name space write:

Strings

This data type allows a much simpler was to manage strings of data then the traditional '\0' terminated character string. For example, the programmer does not have to be concerned that the string will grow to long and the traditional operators apply.

Functions

Here are 4 neat things that C++ gives us with respect to functions. Examples will be in the functions project. These are:

  1. inline functions. These eliminate the need for macros.
  2. Function overloading and default arguments. This allows us to add more features to a function without effecting applications that use it.
  3. Function templates. Allows us to define a whole class of functions.
  4. Pass by reference.

Classes

This section will be devoted to discussion various aspects of classes.

Why Class?????

  1. To tie together data and the functions to manipulate them in a coherent manner. Date/time functions.
  2. To allow for a heriarchy if tools and representation. Example, communications code. Good for reuse.
  3. To separate the implementation from the API.
  4. To allow for a more natural organization in programming. That is organization by data element rather than by function. This is big because of the size of programming projects.
  5. We do NOT use classes to make programs smaller! Just more readable.

Format of a class:

A class declaration has the following format:

Since the public section is used to provide an interface to the user of your class, it usually comes first.

The class can have inline functions rather than prototypes. If it has prototypes, the functions must be implemented in a C++ source files. In the function header, the function name must be prefixed by the class name followed by "::". See the vtime project for an example of a simple class.

Notes

  1. a class definition does not allocate memory.
  2. Can define operators that use the class as argument.
  3. Note about friend functions and classes.
  4. You can defined operators on the class. Will do a + to add seconds onto time.
  5. Example does not check validity of data.

Inheritance

Basically, a class can specify one or more parents. It will inherit the public interface from the parent class. The child is not allowed to mess with the private section of the parent's class. We will create a derived class from the parent classed called stime. This class will keep time in hours, minutes, seconds and microseconds. It is hard to make up good examples for short programs.

Protected data elements and functions in base class accessible. Protected data elements and functions are not accessible by applications that used the base or derived class.

We have been talking about public inheritance. There are other types of inheitance, but that are more restrictive. Very rare to need these.

Virtual Functions

Virtual functions provide a means of letting a base class specify a template of some of the functionality it wants each dirived class to provide. Suppose you wanted to create an PC paint type of program. A good way to create such a class is to have an class that encompasses the concept of a figure. Then have drived classes for each of the figures you want to draw. The base class might look like what I have written below. The "= 0" part of the definition means that these functions are pure virtual functions. That means that each derived class must supply these functions.

class figure {

public:

virtual void move( int deltaX, int deltaY ) = 0;

virtual void erase( ) = 0;

virtual void changeSize( double percentChange );

virtual void color( FigureColor color );

virtual FigureType whatAmI();

etc:

};

Each figure that is introduced would supply implementation of these functions. Your could then have an array of all the objects on the screen,

If there are 100 figures on the screen of various type, we could collor them all blue by:

Put a pure virtual function in the vtime base class and see the error.

Then add it to the derived class. Make it a function to zero out the time.

STL - Standard Template Library

When we write programs we typically find ourselves writing the same code over and over again for certain data structures in different contexts. STL provides template classes (containers) to hold the various data structures and general purpose algorithms to act upon them. The big thing about containers is that they can hold almost any data type as long as you define certain operators. STL was recently made part of the C++ standards.

The following are a list of container supplied by STL.

  1. Vectors (High class arrays.)
  2. Lists (Linked lists.)
  3. stacks, deques, and queues
  4. sets and multisets. (Binary tree structures)
  5. Maps and multimaps. (Binary tree structures with data associated with each key.)

The algorithms supplied are the usual searchs, sorts, replace, inserts, etc.

STL is nice in that they publish big O data for each algorithm.

See the project STL for an example.