Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.

Similar presentations


Presentation on theme: "Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives."— Presentation transcript:

1 Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives

2 CS-1030 Dr. Mark Hornick 2 Before there was C++ there was just plain C No classes in C Instead: structs – more on those later (maybe) No classes = no class methods Instead, there were just functions… Today in C++, they are called global functions E.g. main() is a global function …and variables

3 CS-1030 Dr. Mark Hornick 3 Splitting an program among multiple source (.c) files Scenario: Divide the implementation of a program into two source files: Demo.c defines the main() and getUserInput() and printOutput() functions B.c defines the calculate() function In order to call calculate() from within Demo.cpp, the calculate() function must be declared in Demo.c Likewise, in order to call printOutput() from within B.c, the printOutput() function must be declared in B.c, or someplace else (like B.h)

4 CS-1030 Dr. Mark Hornick 4 Declarations vs Definitions Declarations announce the existence of something to the compiler Declarations can be announced in many places Definitions specify what those “somethings” are Definitions can only be provided once

5 CS-1030 Dr. Mark Hornick 5 Function declarations Syntax [extern] int func1(int, float); This just announces to the compiler that, somewhere, there is a corresponding definition (containing a body) for the function

6 CS-1030 Dr. Mark Hornick 6 Function definitions Syntax int func1(int a, float b) { } This defines what the function actually is

7 CS-1030 Dr. Mark Hornick 7 Global variable declarations Syntax extern int x; This just announces to the compiler that, somewhere, there is a corresponding definition for the variable

8 CS-1030 Dr. Mark Hornick 8 Global variable definitions Syntax int x=10; This defines what the variable actually is Memory for the variable is allocated What about initialization? Not required, but if not supplied, value is undefined

9 CS-1030 Dr. Mark Hornick 9 #include directive Typing a lot of function and variable declarations can be tedious and error-prone Instead, we type those declarations only once in a header file, and merge the declarations in that file with those in the.c file A header file usually has the.h file extension The merging is done via the #include directive: #include A header file can recursively #include other header files This can continue many levels deep, which is often the case

10 CS-1030 Dr. Mark Hornick 10 #include details The #include directive Instructs the compiler to literally include the contents of the specified file Included files are referred to as “header files” Header files often end with.h extension Notable exception: C++ standard library header files Syntax 1: #include Searches directory path(s) specified by the INCLUDE environment variable Syntax 2: #include “file” First searches the current directory (where the.cpp file exists, then searches directory path(s) specified by the INCLUDE environment variable Syntax 3: #include “ ” Searches for the specific file only at the specific location Ex: #include “C:\My Documents\sample.txt”

11 CS-1030 Dr. Mark Hornick 11 C/C++ Compiler The compiler processes each source file (.c) individually Output is an object file (.o) The term “object file” has nothing to do with C++ objects; the naming is a legacy from the era before OO The compiler makes two passes through the source file First pass inspects the file for Preprocessor Directives #include #define #ifdef #pragma Many others… The second pass parses the C statements and (ultimately) generates the.o file.o contains processor-specific instructions References to “other stuff” not found within the original source file

12 CS-1030 Dr. Mark Hornick 12 #define directive In C, used often to alias constant values to mnemonics #define A 1 Equates a symbol named “A” with literal 1 Everywhere A subsequently appears, the preprocessor substitutes the literal value 1

13 CS-1030 Dr. Mark Hornick 13 #ifdef Used for conditional compilation of code #define DEBUG 1 … #ifdef DEBUG for(…) // some C code goes here #endif The code within the #ifdef is compiled only if DEBUG is non-zero

14 CS-1030 Dr. Mark Hornick 14 Preprocessor documentation Search “preprocessor” in the online help


Download ppt "Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives."

Similar presentations


Ads by Google