Separation into 'h and 'cpp Files - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Separation into 'h and 'cpp Files

Description:

Ensure the contents in between will be included for at most once in compilation ... Include the function prototypes (which are defined in test.h) since they are ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 16
Provided by: raym168
Category:

less

Transcript and Presenter's Notes

Title: Separation into 'h and 'cpp Files


1
Separation into.h and .cpp Files
  • COMP103 Lab 6

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
4
ifndef, 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)

5
ifndef, 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
6
Examples 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
7
Examples 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
8
Example Temperature Class
  • Original single .cpp file (original.cpp)
  • Decomposed .h and .cpp files
  • temperature.h
  • temperature.cpp
  • main.cpp

9
Steps to Use Multiple Source Files for Your
Program
  • Select File-gtNew
  • Create a new Win32 Console Application in Projects

10
Steps to Use Multiple Source Files for Your
Program
  • Click OK, Finish and then OK

11
Steps to Use Multiple Source Files for Your
Program
  • Switch to FileView

12
Steps to Use Multiple Source Files for Your
Program
  • Select File-gtNew
  • Create a new C Header File in Files

13
Steps to Use Multiple Source Files for Your
Program
  • Select File-gtNew
  • Create a new C Source File in Files

14
Steps to Use Multiple Source Files for Your
Program
  • Select File-gtNew
  • Create a new C Source File in Files

15
Steps 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
Write a Comment
User Comments (0)
About PowerShow.com