Download presentation
Presentation is loading. Please wait.
1
Lecture 5: Macros, Namespace
Object Oriented Programming (C++) Lecture 5: Macros, Namespace
2
Contents Macros Namespace
3
Macros Macros are very important in C but have far
fewer uses in C++. However, we still need to know it. Declaration : #define macro_name <text> for example: #define NAME rest of line When we write cout<<NAME; it will be replaced by cout<<rest of line;
4
Macros(Cont.) The advantages of the macro:
1) It can improve the readability of the program. 2) It is convenient for the modification of the program. For example: #define FALSE 0 #define TRUE 1 We can write if (result==TRUE) is better than if (result==1). If we want –1 represents false, we can just write: #define FALSE -1
5
Macros(Cont.) Example: #define ONE 1 #define TWO ONE+ONE
#define THREE ONE+TWO void main() { cout<<THREE<<endl; cout<<(TWO)*5<<endl; } Result: 3 10
6
Macros(Cont.) A macro can also be defined to take arguments.
Declaration : #define macro_name(arg_list) text Example: #define MAC(x,y) argument1: x argument2: y When MAC is used, two argument strings must be presented. They will replace x and y when MAC() is expanded. For example, expanded = MAC(foo bar, yuk yuk) will be expanded into expanded = argument1: foo bar argument2: yuk yuk
7
Macros(Cont.) Notes: 1)Macro names cannot be overloaded, and the macro preprocessor cannot handle recursive calls: #define PRINT(a,b) cout<<(a)<<(b) #define PRINT(a,b,c) cout<<(a)<<(b)<<(c) /* trouble?: redefines, does not overload */ #define FAC(n) (n>1)?n*FAC(n1):1 /* trouble: recursive macro */
8
Macros(Cont.) 2) If you must use a macro, enclose occurrences of a macro argument name in parentheses whenever possible. #define SQUARE(a) a*a void main() { int xx = 1; / / local variable int y = SQUARE(xx+2) ; / / y=xx+2*xx+2; that is y=xx+(2*xx)+2 cout<<y<<endl; } The result is 5 not 9. We should define as: #define SQUARE(a) (a)*(a)
9
Macros(Cont.) Conditional Compilation(1)
One use of macros is almost impossible to avoid. The directive #ifdef identifier conditionally causes all input to be ignored until a #endif directive is seen. For example: int f(int a #ifdef arg_ two ,int b #endif ) ; produces
10
Macros(Cont.) Conditional Compilation(2)
Command #if, #elif, #else, #endif etc which are like the if command in C++. #include <iostream.h> #define NUM 100 void main() { #if NUM>=100 cout<<“This line is compiled.\n”; #else cout<<“This line is not compiled.\n”; #endif }
11
Macros(Cont.) Conditional Compilation(3)
Command #ifndef, #define #endif which are used for the definition of the header file. #ifndef _STACK_H_ #define _STACK_H_ //main content #endif
12
Namespace A namespace is a mechanism for expressing logical grouping. That is, if some declarations logically belong together according to some criteria, they can be put in a common namespace to express that fact. When several programmers are working for the same project, they would use the same name for variants of functions. This need to be solved. namespace thus appear to avoid the confliction of names.
13
Namespace(cont.) A namespace is a scope(作用域).
The definition of namespace is: namespace namespace-name { …//name declaration and definition }
14
Namespace(cont.) A namespace must be declared when using its name members. And then we can directly use the name members. eg:using namespace std; Then we can use cout, cin etc. directly.
15
Namespace(cont.) //a1.h namespace a1{ void f1(); } //a2.h
void p(); } //a3.h namespace a3{ void g1(); void g2(); void f2(); void h(); } return
16
Namespace(cont.) int main(){ //a2.cpp f1(); #include “a1.h” f2();
using namespace a1; using namespace a3; static void f3(); int main(){ f1(); f2(); f3();} void f3(){ } void a2::p(){ notes:if there is a function called fn() in a1 and a3,then ,in a2.cpp, as we call fn(),“a1::” or “a3::” must be added in.
17
Namespace(cont.) We can use namespace without name, eg: namespace {
void mesg() cout<<"**********\n"; } void main() mesg(); //ok //...;
18
Namespace(cont.) The global namespace member can be used like this:
“::member_name”
19
using declaration 1、using declaration and using directive:The former is to declare one member in a namespace,the latter is to use the whole namespace,eg: using cpp_primer::matrix; // ok,using declaration using namespace cpp_primer; //ok,using directive 2、using directive expression can be added in any place in a file, including the header of a file. (before #include)、within a function etc. but the lifespan is different as the location of using expression.
20
using declaration 3.Through the using expression, we can define several namespace, and they can be available simultaneously. But this can make the name polluted more. So we should use more using declaration than directives.
21
The end!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.