Title: Separation into 'h and 'cpp Files
1Separation into.h and .cpp Files
2.h (Header) and .cpp Pairs
- xxx.h
- Constants
- Global variables
- Function prototypes
- Class definition
- xxx.cpp
- Function implementation and member function
implementation for those defined in xxx.h
3.h and .cpp Files in a Program
// zzz.h // constants // global variables //
function prototypes // class definitions
include "zzz.h" // zzz.cpp // corresponding
function implementations // corresponding member
function definitions
// yyy.h // constants // global variables //
function prototypes // class definitions
include "yyy.h" // yyy.cpp // corresponding
function implementations // corresponding member
function definitions
// xxx.h // constants // global variables //
function prototypes // class definitions
include "xxx.h" // xxx.cpp // corresponding
function implementations // corresponding member
function definitions
include "xxx.h" include "yyy.h" include
"zzz.h" // main.cpp // main() function
4ifndef, define and endif
- Ensure the contents in between will be included
for at most once in compilation - Otherwise, errors may occur
- You could imagine as
- While the name (TEST_H) is not defined, we will
define it and include the function prototypes,
class definitions, etc (This will happen in the
first inclusion, i.e. include test.h) - While the name (TESET_H) is defined (or found),
we will not include the contents between ifndef
and endif (This will happen in the later
inclusion, i.e. include test.h)
5ifndef, define and endif
- Usually, they are used in the .h files
- Traditionally, the name after ifndef and define
is a special variant of the file name - xxx.h gt XXX_H
- Common usage (in .h files)
ifndef XXX_H define XXX_H // constants, global
variables, function prototypes, class
definition ........... endif
6Examples of .h and .cpp Files
// test.cpp - implementation of functions defined
in test.h include test.h bool func1()
return false
// test.h - function prototypes ifndef
TEST_H define TEST_H bool func1() endif
// all.cpp include ltiostreamgt using namespace
std bool func1() void main() cout ltlt
func1() ltlt endl bool func1() return
false
Code in a single all.cpp file
Separate into 3 files
// program.cpp include ltiostreamgt include
test.h using namespace std void main()
cout ltlt func1() ltlt endl
7Examples of .h and .cpp Files
// test.cpp - implementation of functions defined
in test.h include "test.h" bool func1()
return false
// test.h - function prototypes ifndef
TEST_H define TEST_H bool func1() endif
Preprocessor directives (ensure the function
prototypes in between are included at most once
in the program)
// program.cpp include ltiostreamgt include
"test.h" using namespace std void main()
cout ltlt func1() ltlt endl
Include the function prototypes (which are
defined in test.h) since they are implementing
(test.cpp) or using (program.cpp) them
8Example Temperature Class
- Original single .cpp file (original.cpp)
- Decomposed .h and .cpp files
- temperature.h
- temperature.cpp
- main.cpp
9Steps to Use Multiple Source Files for Your
Program
- Select File-gtNew
- Create a new Win32 Console Application in Projects
10Steps to Use Multiple Source Files for Your
Program
- Click OK, Finish and then OK
11Steps to Use Multiple Source Files for Your
Program
12Steps to Use Multiple Source Files for Your
Program
- Select File-gtNew
- Create a new C Header File in Files
13Steps to Use Multiple Source Files for Your
Program
- Select File-gtNew
- Create a new C Source File in Files
14Steps to Use Multiple Source Files for Your
Program
- Select File-gtNew
- Create a new C Source File in Files
15Steps to Use Multiple Source Files for Your
Program
- You have added 2 .cpp and 1 .h files
- You can now use your normal procedure to write,
compile and run your code