Chapter II. Modular Programming in C++ TCT
Chapter II. Modular Programming in C++ TCT
Chapter II. Modular Programming in C++ TCT
Hà Nội, 2020 1
Chapter II. Modular programming in C++
2
Chapter II. Modular programming in C++
3
The basics of functions
4
The basics of functions
function
sqrt()
isalpha()
atoi()
rand()
strlen()
wcslen()
5
The basics of functions
Function definition
6
The basics of functions
7
The basics of functions
function_name (〈argument 1 , argument 2 , … argument n 〉)
8
The basics of functions
Function declaration
Function definition
9
The basics of functions
• The transferring (throw) of exceptions to the caller function can be enabled or disabled
in function header
return_type function_name (〈parameterlist〉) 〈throw(〈type_list〉)〉
{
〈local definitions and declarations〉
〈statements〉
return 〈expression〉;
}
11
The basics of functions
12
The basics of functions
13
The basics of functions
Parametrizing functions
• Các kiểu dữ liệu có thể sử dụng trong hàm:
(bool, char, wchar_t, short, int, long, long long, float, double, enumeration, reference
and pointer) or structure, union, class or array…
14
The basics of functions
Parametrizing functions
Parameter passing methods
• Passing parameters by value
15
The basics of functions
Parametrizing functions
Parameter passing methods
• Passing parameters by value
If the type qualifier const is placed in the parameter list, we can restrict the modification of the
memory space to which a pointer points ( const double*p) and restrict the modification of the
value of the pointer ( double * const p) within a function.
16
The basics of functions
Parametrizing functions
Parameter passing methods
• Passing parameters by reference
17
The basics of functions
18
The basics of functions
19
The basics of functions
20
The basics of functions
21
The basics of functions
22
The basics of functions
23
The basics of functions
24
The basics of functions
25
The basics of functions
26
The basics of functions
27
The basics of functions
28
The basics of functions
29
Chapter II. Modular programming in C++
30
How to use functions on a more professional level?
Inline functions
Overloading (redefining) function names
Function templates
31
How to use functions on a more professional level?
32
How to use functions on a more professional level?
Kiểm tra vị trí của một giá trị trong một véc tơ
33
How to use functions on a more professional level?
Inline functions
Overloading (redefining) function names
Function templates
34
How to use functions on a more professional level?
35
How to use functions on a more professional level?
36
How to use functions on a more professional level?
Inline functions
Overloading (redefining) function names
Function templates
37
How to use functions on a more professional level?
38
How to use functions on a more professional level?
Function templates
Creating and using function templates
39
How to use functions on a more professional level?
Function templates
Creating and using function templates
40
How to use functions on a more professional level?
Function templates
Some further function template examples
41
Chapter II. Modular programming in C++
42
Namespaces and storage classes
43
Namespaces and storage classes
45
Namespaces and storage classes
46
Namespaces and storage classes
Namespaces
Khi Code đang viết có hàm tên là xyz() và có thư viện khác có sẵn mà
cũng có hàm xyz(). Bây giờ, trình biên dịch không biết phiên bản nào
của hàm xyz() mà bạn muốn sử dụng trong code của mình.
Một namespace trong C++ giúp bạn giải quyết tình huống này và
được sử dụng như là thông tin bổ sung để phân biệt các hàm, lớp,
biến… cùng tên có sẵn trong các thư viện khác nhau. Sử dụng
namespace trong C++, bạn có thể định nghĩa bối cảnh trong đó các
tên được định nghĩa. Về bản chất, một namespace định nghĩa một
phạm vi trong C++.
47
Namespaces and storage classes
Namespaces
The default namespaces of C++ and the scope operator
48
Namespaces and storage classes
Namespaces
Creating and using user-defined namespaces
• Creating namespaces
49
Namespaces and storage classes
50
Namespaces and storage classes
Namespaces
Creating and using user-defined namespaces
• Anonymous namespaces:
Dùng tương tự như biến thường
51
Chapter II. Modular programming in C++
52
Preprocessor directives of C++
Including files
Conditional compilation
Using macros
54
Preprocessor directives of C++
Including files
The following table sums up the keywords and language elements that can be used in header files:
C++ elements Example
Comments // comment
Conditional directives #ifdef MOTOROLA
Macro definitions #define INTEL
#include directives #include <string>
Enumerations enum response {no, yes, maybe};
Constant definitions const double pi=3.1415265;
Namespaces having an identifier namespace nsrand { }
Name declarations struct vector3D;
Type definitions struct complex {double re, im;};
Variable declarations extern double point[];
Function prototypes double Average(double, double);
inline function definitions inline int Sqr(int a) {return a*a;}
template declarations template <class T> T Sqr(T a);
template definitions template <class T> T Sqr(T a) {return a*a;} 55
Preprocessor directives of C++
Including files
The following elements should never be placed in include files:
•definitions of non-inline functions,
•variable definitions,
•definitions of anonymous namespaces.
One part of the elements that can be placed in a header file have to be included
only once in a code. That is why, all header files have to have a special
preprocessing structure based on conditional directives:
56
Preprocessor directives of C++
Including files
Conditional compilation
Using macros
57
Preprocessor directives of C++
Conditional compilation
• Bộ tiền xử lý (Preprocessor) là các directive (chỉ thị), cung cấp chỉ lệnh tới bộ biên dịch
để tiền xử lý thông tin trước khi bắt đầu biên dịch thực sự.:
#if, #ifdef, #ifndef, #elif, #else and #endif.
• - Dùng để kiểm tra/ chạy một đoạn chương trình
58
Preprocessor directives of C++
Conditional compilation
• Instead of the structure above, it is better to use a solution that examines the
definition of the symbol TEST. For that purpose, TEST can be defined without a value,
which can be assigned to it from a parameter of the compiler:
59
Preprocessor directives of C++
Conditional compilation
• Each pair of the following checkings return the same results:
60
Preprocessor directives of C++
Conditional compilation
• If the following structure is used, we can choose between two code parts:
61
Preprocessor directives of C++
Including files
Conditional compilation
Using macros
62
Preprocessor directives of C++
Using macros
Symbolic constants
• Symbolic constants can be created by using the simple form of the #define directive:
63
Preprocessor directives of C++
Using macros
Parameterized macros
64
Preprocessor directives of C++
Using macros
Undefining a macro
• A macro can be undefined anytime and can be redefined again, even with a different
content. It can be undefined by using the #undef directive. Before redefining a macro
with a new content, the old definition always has to be undefined.
The #undef statement does not signal an error if the macro to be undefined does not
exist:
Original source code Substituted code
int main() {
#define MACRO(x) (x) + 7
int main() {
int a = MACRO(12); int a = (12) + 7;
#undef MACRO
a = MACRO(12); //lỗi
a = MACRO(12); //lỗi a = 123
#define MACRO 123
}
a = MACRO
}
65
Preprocessor directives of C++
Using macros
Macro operators
66
Preprocessor directives of C++
Using macros
Macro operators
• By using the ## operator, two syntactic units (tokens) can be concatenated. To achieve
this, the ## operator has to be inserted between the parameters in the body of a
macro.
67
Preprocessor directives of C++
Using macros
Predefined macros
Macro Description Example
__DATE__ String constant containing the date of the compilation. "Oct 02 2013"
__TIMESTAMP__ The date and time of the last modification of the source file in a "Mon Jul 29
string constant" 07:33:29 2013"
__FILE__ String constant containing the name of the source file. "c:\\preproc.cpp"
69