cs246-s11-001-july19

cs246-s11-001-july19 - CS 246(Spring 2011 Section 1 Mewsday...

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

View Full Document Right Arrow Icon
CS 246 (Spring 2011) – Section 1 Mew sday 19 July Namespaces and Software Development
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 246 (Spring 2011) - Section 001 - Tuesday 19 July 2 Today's Agenda 2.27 Namespace 4.2 – 4.4 Selected topics in Software Development
Background image of page 2
CS 246 (Spring 2011) - Section 001 - Tuesday 19 July 3 2.27 Namespace namespace in C++ deals with naming conflicts in complex programs using namespace std; You have been including this in your programs Without it, you would have to say std::cout << “hello, world” or std::vector<int> v; (among other things) Names in a namespace form a declaration region , like the scope of a {...} block
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS 246 (Spring 2011) - Section 001 - Tuesday 19 July 4 Namespace (ctd.) Namespaces do not provide abstraction and encapsulation like the separation between .h/.cc files Defining namespaces namespace First { int k = 12; double a = 3.14; } namespace First { enum Buildings {MC, DC, SLC}; } namespace Second { int k = 42; }
Background image of page 4
CS 246 (Spring 2011) - Section 001 - Tuesday 19 July 5 Accessing namespace contents Access namespace contents by qualifying with the name of the namespace int main() { // Example 1 int value = First:: k + Second:: k; cout << value << endl; int k = First:: k; k += 5; cout << k << endl; cout << First:: k << endl; for (int i=0; i < First:: LAST_ELEMENT; i++) { cout << i << endl; } }
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS 246 (Spring 2011) - Section 001 - Tuesday 19 July 6 A using declaration A using declaration unconditionally introduces an alias into the current scope for a specific entry in a namespace May appear in any scope int main() { // Example 2 int k = 3; for (int i = 0; i < k; i++) { using First::k; cout << k << endl; if ((k + i) % 2 == 0) { using Second::k; cout << k << endl; } } }
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/08/2011 for the course CS 246 taught by Professor Wormer during the Spring '08 term at Waterloo.

Page1 / 20

cs246-s11-001-july19 - CS 246(Spring 2011 Section 1 Mewsday...

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

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