Programming in C++ 3 Ways to Use Namespace Identifiers use a qualified name consisting of the namespace, the scope resolution operator and the desired the identifier alpha std abs( beta); write a using declaration using std: :abs; alpha =abs( beta ) write a using directive locally or globally using namespace std alpha abs( beta
11 3 Ways to Use Namespace Identifiers • use a qualified name consisting of the namespace, the scope resolution operator :: and the desired the identifier alpha = std :: abs( beta ) ; • write a using declaration using std::abs ; alpha = abs( beta ); • write a using directive locally or globally using namespace std ; alpha = abs( beta );
Programming in C++ Variable Declarations and Definitions These allocate memory int somelnt Mfor the global variable int Square(int n) //for instructions in body int result j l/ for the local variable result s n*n return result 12
12 These allocate memory int someInt ; // for the global variable int Square (int n) // for instructions in body { int result ; // for the local variable result = n * n ; return result ; } Variable Declarations and Definitions
Programming in C++ These do NOT allocate memory int Square(int n) //function prototype extern int someInt ∥ someInt is global ∥ variable defined in ∥/ another file 13
13 These do NOT allocate memory int Square (int n) ; // function prototype extern int someInt ; // someInt is global // variable defined in // another file
Programming in C++ Lifetime of a variable o the lifetime of a variable is the time during program execution when an identifier actually has memory allocated to it 14
14 Lifetime of a Variable ❖the lifetime of a variable is the time during program execution when an identifier actually has memory allocated to it
Programming in C++ Lifetime of Local Automatic Variables their storage is created (allocated) when control enters the function local variables are alive while function is executing stheir storage is destroyed (deallocated) when function exits 15
15 Lifetime of Local Automatic Variables ❖their storage is created (allocated) when control enters the function ❖local variables are “alive” while function is executing ❖their storage is destroyed (deallocated) when function exits