{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

T01_Fun - Tutorials 1 2 COMP152 Spring 2010 Basic C Reviews...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Basic C++ Reviews COMP152 Spring 2010 D6, 8, 25
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Outline Namespaces Scope rules Function pointer and array of functions File I/O COMP152 2
Background image of page 2
Unary Scope Resolution Operator Used to access a global variable when a local variable of the same name is in scope Cannot be used to access a local variable of the same name in an outer block Always using the unary scope resolution operator ( :: ) to refer to global variables makes programs easier to read and understand COMP152 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
unary.cpp Sample Output COMP152 4 int number = 7; // global variable named number int main() { double number = 10.5; // local variable named number // display values of local and global variables cout << "Local double value of number = " << number << "\nGlobal int value of number = " << ::number << endl; return 0; } Local double value of number = 10.5 Global int value of number = 7
Background image of page 4
Namespaces Defines scope in which identifiers and variables are placed Namespace members Qualifying a namespace member’s name Precede with the namespace name and the binary scope resolution operator: MyNameSpace::member using directives: using namespace MyNameSpace; Automatically qualifies all members in a namespace Must appear before the names are used in the program Members of namespace MyNameSpace can be used without being qualified using declarations: using std::cout; Automatically qualifies one member of a namespace Brings std::cout into the current scope COMP152 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Namespaces Defining namespaces Keyword namespace A namespace name Can be an unnamed namespace Unnamed namespaces have implicit using directives Body of a namespace is delimited by braces ({}) May be defined at global scope or nested within another namespace Namespace aliases Example: Namespace CPPHTP = CPlusPlusHowToProgram; CPPHTP is an alias for CPlusPlusHowToProgram COMP152 6
Background image of page 6
namespace.cpp (1/3) COMP152 7 using namespace std; // use std namespace int integer1 = 98; // global variable namespace Example // create namespace Example { // declare two constants and one variable const double PI = 3.14159; const double E = 2.71828; int integer1 = 8; void printValues(); // prototype namespace Inner // nested namespace { enum Years { FISCAL1 = 1990, FISCAL2, FISCAL3 }; } // end Inner namespace } // end Example namespace Inform the compiler that namespace std is being used Define namespace Example Define variables in namespace Example Declare a function prototype in namespace Example Define nested namespace Inner
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
namespace.cpp (2/3) COMP152 8 namespace // create unnamed namespace { double doubleInUnnamed = 88.22; // declare variable } // end unnamed namespace int main() { // output value doubleInUnnamed of unnamed namespace cout << "doubleInUnnamed = " << doubleInUnnamed; // output global variable cout << "\n(global) integer1 = " << integer1; // output values of Example namespace cout << "\nPI = " << Example::PI << "\nE = " << Example::E
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 53

T01_Fun - Tutorials 1 2 COMP152 Spring 2010 Basic C Reviews...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online