Object-Oriented Programming

Physical Organization

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Physical Organization

  • Structure of files and directories
  • What is placed in what files
  • Related to structural and syntactic organization

Terminology

  • free function - function not belonging to a class
  • method - member function
  • function - Can refer to a free function or a method
  • With used with the terminology function and method, function is referring to a free function
  • Otherwise, the term function is referring to both free functions and methods
  • To clarify, use the term free function when there is potential ambiguity

main program file

  • Contains main() function
  • .cpp extension
  • Only one per program, but can have more than one per project
  • compiled

include/declaration file

  • .hpp extension
  • Typically paired with an implementation file
  • free function declarations
  • class definitions
  • method declarations
  • Never compiled, always included

implementation file

  • .cpp extension
  • function definitions
  • method definitions
  • compiled

srcFacts

  • srcFacts.cpp - main program
  • refillBuffer.hpp - include file for refillBuffer() function
  • refillBuffer.cpp - implementation file for refillBuffer() function
  • xml_parser.hpp - include file for XML parsing functions
  • xml_parser.cpp - implementation file for XML parsing functions

Include/Implementation pairs

  • Intended for use in multiple programs
  • At a minimum, a “use” program and a test program
  • How external libraries to your program are developed and organized

Include Organization

Implementation Organization

Include File Organization

  • To use this split() the client program, e.g., main.cpp only has to include a single file
  • Include file, e.g., split.hpp, only includes the minimum necessary for the declaration.
  • Implementation file, e.g., split.cpp, contains includes for files used only in the implementation

Include file rules

  • Always include the include file, e.g., split.hpp first of the includes in the corresponding implementation file, e.g., split.cpp
  • Why? It verifies that the include file does not depend on previous includes
  • Includes files could be put in any order and still must work
  • In the include file, only include the files needed for the declaration not all the files needed for the implementation
  • E.g., We did not include <cstring> in split.hpp because it is only used in the implementation.
  • If you use it in a file, declare the proper include file
  • Do not depend on indirect includes
  • If you remove the use of a library from the code in a file, then remove the corresponding include file

Relying on an Indirect Include

Builds
Doesn't Build

What about constants?

  • If a constant is for the implementation, then it belongs in the implementation file, e.g., split.cpp
  • If a constant is for the client to use, then it belongs in the include file, e.g., split.hpp

Preprocessor

  • #include "split.hpp" directive
  • Copies the entire contents of the file, e.g., split.hpp, into the current file
  • View this form using the using the compiler option -E
  • split

Preprocessor Directives

Directive  
#include "split.hpp" Inserts the file
#define INCLUDED_SPLIT_HPP Defines a preprocessor symbol
#define READ _read Defines a preprocessor symbol with a value
#ifndef INCLUDED_SPLIT_HPP Starts a section if the symbol is not defined
#endif Ends a #if, #ifdef, or #ifndef section
#if Starts a section if expression is true
#else Else of a section
#elif Else if
#ifdef Starts a section if the symbol is defined
#undef Undefines a preprocessor symbol
#pragma Special non-standard compiler features
#error C++17 required Outputs an error message

CPP Idiom: Platform Specifics

CPP Idiom: Require C++ Version