Download presentation
Presentation is loading. Please wait.
Published byKory Thompson Modified over 9 years ago
1
#include guards Practical session #2 Software Engineering 094219
2
Modular programming
3
Header file = interface Declarations of functions and classes. Definitions of classes, constants and types. Inline functions. CPP file = implementation Definitions of functions (class members or regular functions ). Initialization of static class members. In order to use the identifiers from header file – it must be included.
4
#include Pre-compiler command that inserts the included file in place of #include. #include - standard library #include “filename.h” - your own header file
5
#include - rule 1 typedef std::vector int_vector; //Needs #include but has no //such include File1.h #include ….. File2.h #include “File2.h” #include “File1.h” Usage1.cpp OK #include “File1.h” #include “File2.h” Usage2.cpp Compilation error! Every header file must have all #includes it requires !!! Otherwise the order of includes will matter and the files are not independent!
6
#include - rule 1 typedef my_int* int_vector; //Needs my_int but has no //such include File1.h typedef int my_int; ….. File2.h #include “File2.h” #include “File1.h” Usage1.cpp OK Compilation error! #include “File1.h” #include “File2.h” Usage2.cpp Every header file must have all #includes it requires !!! Otherwise the order of includes will matter and the files are not independent!
7
#include - a problem #include “File1.h” #include “File3.h” //Needs code C and B Usage1.cpp Compilation error! Redefinition of class A “File2.h” is included twice ! => class A is defined twice ! class A { }; //More code File2.h #include “File2.h” //Code C File1.h #include “File2.h” //Code B File3.h
8
#include - a solution #include “File1.h” #include “File3.h” //Needs code C and B Usage1.cpp OK #ifndef FILE2_H #define FILE2_H class A { }; //More code #endif File2.h #include “File2.h” //Code C File1.h #include “File2.h” //Code B File3.h Include guards Every header file should have include guards ! To avoid double or cyclic inclusion! These files should have include guards too !
9
C++ variables and types
10
Agenda Variables and types Reference 10
11
Variable Variable is a place (a piece of memory) for data storage, in C++: variable=object. Variable has a name = identifier. Variable has a type, that determines: The size in memory. The values that can be stored in it. The operations that can be applied to it. Variables should be initialized (good practice ). Initialization syntax: type identifier (initial_value) ; //example: int k(5); type identifier = initial_value; //example: int k = 5; 11 The same Example: double num ( 0.0 );
12
Types in C++ 12 1. Primitive types. Examples: bool, int, double, char… 2. Compound (or composite ) types: Any type that is not primitive. Any type that made up of primitive types or other compound types. Examples: Arrays., Pointers, references, Classes, And there are more… C++ is a statically typed language each variable has to have a type before usage and this type cannot be changed!
13
Primitive built-in types Minimum sizeMeaningType NABooleanbool 8 bitsCharacterchar 16 bitsWide characterwchar_t 16 bitsShort integershort 16 bitsIntegerint 32 bitsLong integerlong 32 bits (6 p)Single-precision floating pointfloat 64 bits (10 p)Double-precision floating pointdouble 128 bits (10 p)Extended-precision floating pointlong double 13 We can find out the actual size of a variable by using sizeof operator
14
Scope and local variable The scope (or simply the “{ }” braces) of an identifier, is the part of the program over which the identifier can be seen and used. Local variable is a variable which defined inside a function. Local variable can be accessed only from the scope where it was defined and cleared automatically at the end of the scope. Example (with compiler errors) : 1. void average() 2. { { 3. for ( int i =0 ; i < 5 ; i++ ) { 4. int x, sum = 0 ; 5. std::cout << “Enter "<< i+1 <<"'th integer:“ << std::endl; 6. std::cin >> x ; 7. sum += x ; } 8. } 9. std::cout<<" The average: “<< sum / i <<std::endl; 10. } What is the error and how can we fix it? 14
15
Global variable defined in the main scope – outside the scope of all functions/classes. Examples : 1. #include 2. //Global scope 3. int x = 20; 4. void incX(){//Local scope 5. // int x = 10 ; 6. x ++; 7. } 8. int main(){ 9. std::cout<<" Global x = "<< x <<std::endl; 10. incX(); 11. std::cout<<" Global x + 1 = "<< x <<std::endl; 12. system("pause"); 13. return 0 ; 14. } Global variable What will be the result of adding this code line (5)? 15
16
Constant ( const) variable Constant ( const ) variable must be initialized at its definition point and cannot be changed after its definition. Example : 1. void main(){ 2. const int cInt = 5 ; 3. const double cDoub ; //ERROR 4. cInt ++ ; //ERROR 5. cDoub +=.3 ; // ERROR 6. std::cout << cInt << std::endl ; 7. system("pause"); 8. } 16
17
Agenda Variables and types Reference 17
18
Reference variable Reference type can be thought as alternative name for a variable. Reference must be initialized at definition time. Example : 1. long index = 10 ; 2. long &refIndex = index ; 3. std::cout<< ++refIndex << endl; 4. std::cout<< index << endl; 5. long &refIndex2 ; //ERROR 6. long &refIndex3 = 10 ; //ERROR Reference can’t be rebind after definition. refIndex is a reference to index forever. Whenever we use refIndex (after we created it) we access the object referenced. 18
19
const reference Reference can be const: you cannot change the object via the const reference. long longNum ; const long& longRef = longNum; //correct longRef = 5; //ERROR The opposite is not correct: const object can have only const references or pointers to it const long longNum (5); long& longRef = longNum; // ERROR. //Because we could change const object //via non const reference! 19
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.