1 / 14

C++ Global functions

C++ Global functions. Declarations & Definitions Preprocessor Directives. 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…

Télécharger la présentation

C++ Global functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Global functions Declarations & Definitions Preprocessor Directives 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 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) 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 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 CS-1030 Dr. Mark Hornick

  6. Function definitions • Syntax • int func1(int a, float b){<C++ statements go here…>} • This defines what the function actually is 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 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 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 <file.h> • A header file can recursively #include other header files • This can continue many levels deep, which is often the case 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 <file> • 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 “<absolute path>” • Searches for the specific file only at the specific location • Ex: #include “C:\My Documents\sample.txt” 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 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 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 CS-1030 Dr. Mark Hornick

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

More Related