The goal of this presentation is to get an understand of some of the more advanced C++ programming concepts.
This section will present some of the new data types available in C++. The example program dataType will demonstrate these.
This data type allows use to use variables that may be "true" or "false".
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.
This data type allows use to specify doubly sized doubles. That is 128 bit floating point numbers. MS maps this into 10 bytes.
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
using namespace std;
To define a name space write:
namespace [identifier] { stuff }
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.
Here are 4 neat things that C++ gives us with respect to functions. Examples will be in the functions project. These are:
This section will be devoted to discussion various aspects of classes.
A class declaration has the following format:
class duck { // duck is the name of the class
// Any variables and functions declared here are not accesible to the user.
public:
// Any variables and functions declared here are accessible to the user.
// Generally, variables are not declared to be public.
private:
// Any variables and functions declared here are not accesible to the user.
};
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
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 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,
figure figScreen[100];
If there are 100 figures on the screen of various type, we could collor them all blue by:
for( int i = 0; i < 100; i++ ) {
figScreen[i].color( Blue );
}
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.
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.
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.