C++
Programming By Specification


Programming by specification separates the Design from the Implementation.   The code design is transferred into the header files of an application.  This file contains the "What" is to be done.  After the design phase is complete, programmers take the header files and implement the design.

A single class is dedicated to a .h and a .cpp file  This module contains a main function to unit test the component.  The design uses conditional compiler directives to compile a module in unit test mode or in system mode.

Specification
Header File *.h
"The What"
Implementation
Code File *.cpp
"The How"
dice3.jpg (4784 bytes)


There are two important compiler directives, the header wrapper and the unit test driver wrapper. 

The header wrapper keeps the compiler from seeing multiply defined symbols during the link phase.

File foo.h

#ifndef FOO_H
#define FOO_H

 // specification code here

#endif

The unit test driver wrapper hides the unit test main function from the system main function.

File foo.cpp

// implementation code here
#include foo.h
#ifdef FOO_TEST
int main ( void )
{
    ...// test driver code
  return 0;
}
#endif

We shall now implement, in C a Dice object.  This new data type is created with a structure and declared in the dice.h file.  Files, client.c, which will use a Dice type will have to #include the specification so that the compiler will know about the date type and its functions.

C
Dice Specification - c/dice.h
dice2.jpg (2069 bytes)
Dice Implementation - c/dice.c
Client code to the unit Dice object - c/client.c

We shall now implement, in C++ a Dice object.  This new data type is created with a class declared in the dice.h file.  Files like client.cpp, which will use a Dice type will have to #include the specification so that the compiler will know about the date type and its functions.

The difference between these two implmentations illustrates the profound improvements that C++ has over C.  C++ encapsulates the object by binding the data and the methods into a single entity called the class.  Classes by default keep there information private to the outside world, where C structures make their information public (any client can access the internal components).

C++
Dice Specification - cpp/dice.h

Dice Implementation - cpp/dice.cpp
Client code to the unit Dice object - cpp/client.cpp

WorkShop:

Build a Win32 Console application for both of these examples.   Download these files.
Compile, Link and Run.


Chapter: 2

This is pretty much a review of basic C control structures.  You should fully understand how to program the control structures which are presented.


05/09/01 09:31 PM