The CMT++ example has the following
4 source files (stack.h, stack.cpp, demofile.h, demofile.cpp):
stack.h:
// ----------------------------------------------------------------
//
// Class : STACK from STACK.H
// Parents :
// Friends :
// Part of :
// Created : 12 Oct 1990 by A.C.Coder
// Abstract : This module implements a dynamic stack for integers.
// Other stacks can be easily constructed by changing
// the defintion of STACK_ITEM type. Note: STACK_ITEM
// should not be any array type.
// Revision : 1.0, 12 Oct 1990 12:00:00, by ACC
//
// Copyright (C) 1990 Sample Software Ltd.
// ----------------------------------------------------------------
// Revision history
//
// 1.0: Initial revision
//
// ----------------------------------------------------------------
#ifndef STACK_H
#define STACK_H
// ------------------------- PUBLIC TYPES -------------------------
//
// STACK_ITEM: The items stored in the stack are of this type
//
typedef int STACK_ITEM;
// ----------------------- CLASS DECLARATION ----------------------
class Stack
{
public:
Stack();
~Stack();
void clear();
unsigned height();
void push(
STACK_ITEM item);
STACK_ITEM pop();
STACK_ITEM top();
protected:
STACK_ITEM &element(unsigned index);
private:
unsigned myheight; // current height of stack
unsigned size; // current size (max. height)
STACK_ITEM *value; // the data in stack
};
// ------------------ INLINE MEMBER DEFINTIONS -------------------
#endif
stack.cpp:
// ----------------------------------------------------------------
//
// Class : STACK from STACK.CPP
// Parents :
// Friends :
// Part of :
// Created : 12 Oct 1990 by A.C.Coder
// Abstract : This module implements a stack.
// Revision : 1.0, 12 Oct 1990 12:00:02, by ACC
//
// Copyright (C) 1990 Sample Software Ltd.
// ----------------------------------------------------------------
// Revision history
//
// 1.0: Initial revision
//
// ----------------------------------------------------------------
// -------------------- APPLICATION INCLUDES ----------------------
#include "stack.h"
// ----------------------- PRIVATE CONSTANTS ----------------------
//
// - SIZE_STEP: defines the number of new stack elements allocated
// each time the stack becomes full
//
int const SIZE_STEP = 40;
// ----------------------------------------------------------------
//
// Function : Stack::Stack
// Description : Initializes the private members, all to 0. Memory
// is not allocated before the first push.
// Updates : - height
// - size
// - value
//
// ----------------------------------------------------------------
Stack::Stack():
myheight(0),
size(0),
value(0)
{
// Nothing
}
// ----------------------------------------------------
//
// Function : Stack::~Stack
// Description :
// Updates : - value
//
// ----------------------------------------------------
Stack::~Stack()
{
delete value;
}
// ----------------------------------------------------
//
// Function : Stack::clear
// Description :
// Updates : - height
//
// ----------------------------------------------------
void Stack::clear()
{
myheight = 0;
}
// ----------------------------------------------------
//
// Function : Stack::empty
// Description :
// Updates :
//
// ----------------------------------------------------
unsigned Stack::height()
{
return myheight;
}
// ----------------------------------------------------
//
// Function : Stack::push
// Description :
// Updates : - height
// - size
// - value
//
// ----------------------------------------------------
void Stack::push(
STACK_ITEM item)
{
if (myheight >= size)
{
size += SIZE_STEP;
STACK_ITEM *new_value = new STACK_ITEM[size];
for (unsigned i = 0; i < myheight; i++)
new_value[i] = value[i];
delete value;
value = new_value;
}
value[myheight++] = item;
}
// ----------------------------------------------------
//
// Function : Stack::pop
// Description :
// Updates : - height
//
// ----------------------------------------------------
STACK_ITEM Stack::pop()
{
STACK_ITEM item;
if (myheight > 0)
item = value[--myheight];
return item;
}
// -----------------------------------------------------
//
// Function : Stack::top
// Description :
// Updates :
//
// -----------------------------------------------------
STACK_ITEM Stack::top()
{
STACK_ITEM item;
if (myheight > 0)
item = value[myheight - 1];
return item;
}
STACK_ITEM &Stack::element(unsigned index) { return value[index]; }
demofile.h:
// This code just demonstrates what CMT++ calculates of various
// C/C++ language constructs. This file compiles ok, but as
// executable code this is nonsense.
extern int a;
class MyClass {
MyClass() {
a = 5;
}
~MyClass() {
a = 0;
}
int foo1(int i);
int foo2(int i);
};
int SomeFunction();
demofile.cpp:
// This code just demonstrates what CMT++ calculates of various
// C/C++ language constructs. This file compiles ok, but as
// executable code this is nonsense.
#include "demofile.h"
int a;
int MyClass::foo1(int i) {
if (i > 5 || i > 6 || i > 7 || i > 8 || i > 9) {
a = a + i;
}
return a;
}
int MyClass::foo2(int i) {
if (i > 10) {
a--;
} else {
a = a - i;
}
return 0;
}
int SomeFunction() {
if (a > 0) {
switch (a) {
case 0:
case 1:
case 2:
a++;
break;
case 3:
a--;
break;
default:
a = 0;
}
}
return a == 0 ? 100 : 200;
}