Download presentation
Presentation is loading. Please wait.
1
What Is? function predefined, programmer-defined
arguments, (formal) parameters return value function call, function invocation caller, callee function definition head, body function prototype (declaration) expanded form, abbreviated form local variables, global constant, scope call-by-value
2
Program in Multiple Files
when one file is not enough
3
(Non) Executable Statements
all C++ statements are divided into executable and non-executable executable - some corresponding machine code is generated by the compiler example: assignment statements, looping/branching constructs, function invocations non-executable - no machine code generated example: function prototypes, variable and constant declarations, #include directives global constant declarations may look like executable - they are not: const double pi=3.14; the compiler may substitute 3.14 for every occurrence of pi in the program
4
Include Files #include directives tell the compiler to include specified file. The files included are also called header files and commonly have extensions .h two forms: #include <filename> - file is found in standard system-dependent location #include ”filename.h” - file is found in the same directory as the rest of the code include files may also contain include directives what to put in include files: non-executable statements what not to put in include files: executable statements, function definitions purpose of include files: centralize declarations
5
Program in Multiple Files
large programs are usually kept in multiple files reasons: easy to maintain can be compiled separately functions are usually grouped into files by their purpose: functions dealing with one particular part of program are kept in one file scope rule: function, variable, constant has to be declared before use what if declaration is in a different file? program is structured as follows: program file (extension .cpp) - contains function definitions include file (extension .h) - contains corresponding function prototypes, global constant and variable declarations if function A defined in file AA.cpp needs to call a function B which is defined in a different file BB.cpp - the corresponding header file BB.h is included in file AA.cpp
6
Example Program in Multiple Files
#include "add1.h" // adds 1, // returns added value int add1(int n) { return n + 1; } add1.cpp add1.h // adds one int add1(int); // uses the function add1 // defined in a separate file #include <iostream> #include "add1.h" int main() { // get the number cout << "Enter a number: "; int n; cin >> n; // find the number plus 1 int newn = add1(n); // print out the number plus 1 cout << newn << endl; } add1test.cpp
7
Separate Compilation separate compilations source program (add1.cpp)
Include files (add1.h, iostream) link object file with standard object files and other object files to produce an executable program add include files check object file (add1.o) file unit for legal syntax and compile it into an object file standard libraries compilation executable program
8
Multiple Inclusion Protection
each definition (e.g. global constant def.) can be encountered only once during compilation when definition is placed in a header file, it may be included multiple times header file must structured so it is safe in case of multiple inclusion; term – multiple inclusion protection mechanism - preprocessor directives #define name value textual substitution of name by value do not use #define instead of global constants problem: #define press 50+5 int myvar = press * 20; // changes order of operations value may be skipped: #define BIG_NAME #ifdef name - true if name defined, #ifndef name - true if not #endif - completes #if header file myheader.h containing definitions usually has the following structure: #ifndef MYHEADER_H #define MYHEADER_H // text of the header file goes here #endif
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.