[Prev] [Top]

No Title

Appendix B - Example Source Code


// ============================================================================

// ttemplate.h

// ============================================================================

#ifndef TTEMPLATE_H

#define TTEMPLATE_H

// Unix Version: $Id$

// PC Version: $Header: $

// Note the copyright notice below is in copyright.h

/*

static char copyright[] =

"Copyright (c) 1996, Scientific and Engineering Software , "

"Inc. All rights reserved worldwide. This program or documentation "

"is supplied pursuant to and its use is governed by a license "

"agreement from Scientific and Engineering Software , Inc. and "

"contains confidential information of Scientific and Engineering "

"Software , Inc. Disclosure and copying are subject to "

"restrictions contained in such agreement and any subsidiary "

"agreement.";

*/

/*

PURPOSE

An explanation of the purpose and general usage for this class

should go here. All major concepts needed to understand the class

should be here. These include concepts, assumptions, example

intended use, and testing information.

*/

#ifndef TMYOBJECT_H

#include "TMyObject.h"

#endif

// ............................................................................

//

// C o n s t a n t s

//

// ............................................................................

// C++ constants are preferred over defines due to the type-safety

// provided. The use of defines should be limited. Note that const

// implies static here. (The compiler will probably optimize these).

//

// Note: const char* is not static, but const char* const is

const int kMyWinWidth = 0; // Constants start with k

// ............................................................................

//

// T y p e s

//

// ............................................................................

// Enums provide a degree of safety and are preferred over define and int.

// The compiler will warn of switch statements which do not cover all

// enum values.

//

// Note that enums always start with a small "e". The values should have

// a unique prefix reflecting the enum type name. Note you should also

// nest enums inside class declarations where appropriate.

enum eRasterOp { roReplace, roInvert, roOverlay, roErase };

// ............................................................................

//

// G l o b a l V a r i a b l e s

//

// ............................................................................

// Forward declarations provide a way to cut down the include-file

// dependencies. They handle pointers and references effectively.

class TMyObject; // forward class declaration

extern TMyObject* gObjPointer; // compiles without knowing class

// ............................................................................

//

// P u b l i c P r o t o t y p e s

//

// ............................................................................

// This is an example of a public function which is implemented in the

// cc file corresponding to this h file.

// M y P u b l i c F u n c t i o n

//

// This function takes an integer "much" which represents the number of

// characters to allocate and fill with zeros. The resulting pointer into

// the heap is returned. Clients are responsible for for deleting the

// memory (using delete [] ptr, not free).

//

// If the new operation fails, NULL is returned.

//

// PRECONDITIONS: much > 0

//

extern char* MyPublicFunction (int much, const TMyObject& mobj);

// Note that you should be passing objects by const reference or reference.

// Avoid passing objects by value!

// ............................................................................

//

// T T e m p l a t e

//

// ............................................................................

// This is a sample class illustrating many of the coding standards. This

// is an example class only. Its purpose is to convey information about

// accepted coding standards. It does not collaberate with any other

// classes.

class TTemplate : public TObject

{

public:

// Constructor should bring object to a consistent state, zeroing

// pointers, for example.

//

TTemplate();

// May not be necessary, but ALWAYS should be virtual. These should

// handle deleting memory allocated by this object (ie, fHeap).

virtual ~TTemplate() ;

// This is an example of a comment for a public method.

//

// PRECONDITION: fData != NULL

//

void doSomething ();

// These are examples of access functions.

// Written this way, the compiler treats them as "inline".

int data() const;

void setData (int newValue);

private:

int fData; // a private data element

char* fHeap; // this implies destructor needed!

// The copy constructor and assignment operator typically can

// be declared, but not defined to prevent value-based usage of

// this class!

//

TTemplate (const TTemplate&);

TTemplate& operator = (const TTemplate&);

friend TWhatever :: method (args); // use sparingly

}; // TTemplate

// Hint: putting the name of the class or method as a trailing comment

// to the closing brace helps developers browsing unfamiliar code.

inline int TTemplate :: data() const { return fData ; }

inline void TTemplate :: setData (int newValue) { fData = newValue; }

#endif

// ============================================================================

// ttemplate.cpp

// ============================================================================

#define TTEMPLATE_CPP

static const char* const kVersionId = "$Id$"; // For Unix

static const char* const kVersionId = "$Header: $"; // For PC

#ifdef COPYRIGHT

static const char copyright[] =

"Copyright (c) 1996, Scientific and Engineering Software , "

"Inc. All rights reserved worldwide. This program or documentation "

"is supplied pursuant to and its use is governed by a license "

"agreement from Scientific and Engineering Software , Inc. and "

"contains confidential information of Scientific and Engineering "

"Software , Inc. Disclosure and copying are subject to "

"restrictions contained in such agreement and any subsidiary "

"agreement.";

#endif

// We have recently changed our include conventions from a flat include

// structure to a tree-oriented include structure (see later description).

#include <stdio.h>

#ifndef TTEMPLATE_H

#include "ttemplate.h"

#endif

// ............................................................................

//

// G l o b a l V a r i a b l e s

//

// ............................................................................

TMyObject* gObjPointer; // global pointer

// ............................................................................

//

// P u b l i c F u n c t i o n s

//

// ............................................................................

// All functions should begin with a capital letter. All methods should

// begin with a lowercase letter.

// M y P u b l i c F u n c t i o n

//

// This should document the purpose for this public function. Note that

// unused arguments should have their names removed (or commented out) to

// avoid compiler warnings.

//

void MyPublicFunction (int x, int)

{

FUNCTION (MyPublicFunction);

// implementation here

} // MyPublicFunction

// ............................................................................

//

// P r i v a t e F u n c t i o n s

//

// ............................................................................

// Note that all private functions should begin with "static" to avoid name

// space pollution and as a ready indication that this is private to this file.

// M y P r i v a t e F u n c t i o n

//

// This should document the purpose for this private function.

//

static void MyPrivateFunction (int& testing, int one, int two, int* three)

{

FUNCTION (MyPrivateFunction);

// implementation here

} // MyPrivateFunction

// ............................................................................

//

// T T e m p l a t e

//

// ............................................................................

// c o n s t r u c t o r

//

TTemplate :: TTemplate () : fData(NULL), fHeap (new char[100])

{

METHOD (TTemplate::TTemplate);

} // TTemplate

// d e s t r u c t o r

//

TTemplate :: ~TTemplate ()

{

METHOD (TTemplate::destructor);

delete [] fHeap;

} // ~TTemplate

// ..................................................................

// Note that in the implementation files, we put the methods in

// alphabetical order following the constructors and destructors.

// ..................................................................

// d o S o m e t h i n g

//

void TTemplate :: doSomething ()

{

METHOD (TTemplate :: doSomething);

PRECONDITION (fData != NULL, "valid data");

// implementation here

} // doSomething


No Title
[Prev] [Top]

Generated with CERN WebMaker

This site was grabbed using the TRIAL version of Grab-a-Site. This message does not appear on a licensed copy of Grab-a-Site.