#include guards Practical session #2 Software Engineering 094219.
-
Upload
kory-thompson -
Category
Documents
-
view
219 -
download
2
Transcript of #include guards Practical session #2 Software Engineering 094219.
![Page 1: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/1.jpg)
#include guards
Practical session #2
Software Engineering 094219
![Page 2: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/2.jpg)
Modular programming
![Page 3: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/3.jpg)
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.
![Page 4: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/4.jpg)
#include Pre-compiler command that inserts the
included file in place of #include. #include <filename> - standard library #include “filename.h” - your own header
file
![Page 5: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/5.jpg)
#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
OKCompilation
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!
![Page 6: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/6.jpg)
#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
![Page 7: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/7.jpg)
#include - a solution
#include “File1.h”#include “File3.h”//Needs code C and B
Usage1.cpp
OK
#ifndef FILE2_H#define FILE2_Hclass 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 !
![Page 8: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/8.jpg)
C++ variables and types
![Page 9: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/9.jpg)
Agenda
Variables and types
Reference
10
![Page 10: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/10.jpg)
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 sam
e
Example:double num
( 0.0 );
![Page 11: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/11.jpg)
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!
![Page 12: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/12.jpg)
Primitive built-in types
Minimum size
Meaning Type
NA Boolean bool
8 bits Character char
16 bits Wide character wchar_t
16 bits Short integer short
16 bits Integer int
32 bits Long integer long
32 bits (6 p) Single-precision floating point
float
64 bits (10 p) Double-precision floating point
double
128 bits (10 p) Extended-precision floating point
long double
13
We can find out the actual size of a
variable by using sizeof operator
![Page 13: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/13.jpg)
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
![Page 14: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/14.jpg)
Global variable defined in the main scope – outside the scope of all functions/classes.
Examples:
1. #include <iostream>2. //Global scope3. int x = 20;4. void incX(){//Local scope5. // 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
![Page 15: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/15.jpg)
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 ; //ERROR4. cInt ++ ; //ERROR5. cDoub += .3 ; // ERROR6. std::cout << cInt << std::endl ;7. system("pause");8. }16
![Page 16: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/16.jpg)
Agenda
Variables and types
Reference
17
![Page 17: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/17.jpg)
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 ; //ERROR6. 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
![Page 18: #include guards Practical session #2 Software Engineering 094219.](https://reader036.fdocuments.us/reader036/viewer/2022082505/56649f585503460f94c7ddfb/html5/thumbnails/18.jpg)
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