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 Multiple function application Demo

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

5 CS-1030 Dr. Mark Hornick 5 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

6 CS-1030 Dr. Mark Hornick 6 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

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

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

9 CS-1030 Dr. Mark Hornick 9 Global variable definitions Syntax int x=10; This defines what the variable actually is Memory for the variable is allocated What about initialization?

10 CS-1030 Dr. Mark Hornick 10 #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.cpp 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

11 CS-1030 Dr. Mark Hornick 11 #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”

12 CS-1030 Dr. Mark Hornick 12 C/C++ Compiler The compiler processes each source file (.cpp) individually Output is an object file (.obj) 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.obj file.obj contains processor-specific instructions References to “other stuff” not found within the original source file

13 CS-1030 Dr. Mark Hornick 13 #define directive In C, used often to alias constant values to mnemonics Still used this way often by C++ programmers; not recommended C++ has a better language syntax using the const keyword More appropriate use in C++ is for conditional compilation

14 CS-1030 Dr. Mark Hornick 14 #ifdef Used for conditional compilation of code

15 CS-1030 Dr. Mark Hornick 15 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