1 Lecture Material Namespaces. 2 There is only one global namespace In programs written by many persons, or using the libraries written by others, there.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Namespaces. Calculator Case-study Consider a simple desk calculator. It can be viewed as being composed of four parts: –The lexer, composing tokens out.
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
Lecture 9 Concepts of Programming Languages
1 CSC241: Object Oriented Programming Lecture No 07.
Introduction to C++ Systems Programming.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Namespaces.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
CS 213 Fall 1998 Namespaces Inline functions Preprocessing Compiling Name mangling Linking.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Looping and Counting Lecture 3 Hartmut Kaiser
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Functions in C++ Default arguments Overloading Inlining Call-by-reference Scope of variables, static, extern namespace new and delete assert.
Object Oriented Programming Elhanan Borenstein Lecture #5.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Function Overloading and References
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
SNU OOPSLA Lab. Chap8. Namespaces and Exceptions © copyright 2001 SNU OOPSLA Lab.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Namespace יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר 2016 יום ראשון 13 נובמבר.
Operator Overloading.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
C++ Lesson 1.
Objectives In this chapter, you will:
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Programming with ANSI C ++
Introduction to C++ Systems Programming.
Object-Oriented Programming (OOP) Lecture No. 21
Lecture 4-7 Classes and Objects
Lecture 9 Concepts of Programming Languages
Namespaces CSCE 121 J. Michael Moore
CS212: Object Oriented Analysis and Design
More About Data Types & Functions
Built-In (a.k.a. Native) Types in C++
Defining New Types Lecture 22 Hartmut Kaiser
Overview of C++ Overloading
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
Objectives In this chapter, you will:
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS1 Introduction to C++.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

1 Lecture Material Namespaces

2 There is only one global namespace In programs written by many persons, or using the libraries written by others, there is a risk of name collision Traditionally, the problem is solved by using prefixes unique for each library // library1.h const double LIB_VERSION = 1.204; // library2.h const int LIB_VERSION = 3; // main.c #include // library1.h const double library1_LIB_VERSION = 1.204; // library2.h const int library2_LIB_VERSION = 3;

3 Namespaces Generally, the namespaces replace the identifier prefixes Instead of we write: const double sdmBOOK_VERSION = 2.0;// in this library, class sdmHandle {... }; // each symbol begins sdmHandle& sdmGetHandle(); // with "sdm" namespace sdm { const double BOOK_VERSION = 2.0; class Handle {... }; Handle& getHandle(); }

4 Using Namespaces void f1() { using namespace sdm; // make all symbols in sdm // available w/o qualification // in this scope cout << BOOK_VERSION;// okay, resolves to sdm::BOOK_VERSION... Handle h = getHandle(); // okay, Handle resolves to // sdm::Handle, getHandle... // resolves to sdm::getHandle } void f2() { using sdm::BOOK_VERSION; // make only BOOK_VERSION // available w/o qualification // in this scope cout << BOOK_VERSION; // okay, resolves to // sdm::BOOK_VERSION... Handle h = getHandle(); // error! neither Handle // nor getHandle were... // imported into this scope } void f3() { cout << sdm::BOOK_VERSION;// okay, makes BOOK_VERSION // available for this one use... // only double d = BOOK_VERSION; // error! BOOK_VERSION is // not in scope Handle h = getHandle(); // error! neither Handle // nor getHandle were... // imported into this scope }

5 Using Namespaces In case of ambiguity, the scope resolution operator :: can be used namespace AcmeWindowSystem {... typedef int Handle;... } void f() { using namespace sdm; // import sdm symbols using namespace AcmeWindowSystem;// import Acme symbols... // freely refer to sdm // and Acme symbols // other than Handle Handle h; // error! which Handle? sdm::Handle h1; // fine, no ambiguity AcmeWindowSystem::Handle h2; // also no ambiguity... }

6 Anonymous Namespaces An anonymous namespace repaces the usage of keyword static before a global identifier External access to the anonymous namespace is possible due to an implicit using directive. The above declaration is equivalent to the following: $$$ is some name which is unique in the scope, where this namespace is defined #include "header.h" namespace { int a; void f() {/*... */} int g() {/*... */ } } #include "header.h" namespace $$$ { int a; void f() {/*... */} int g() {/*... */ } } using namespace $$$;

7 Searching for Names A function with an argument of type T is frequently defined in the same namespace as T. If the function cannot be found in the context it is used, the namespaces of its arguments are searched. This rule makes possible to avoid the explicit qualifying of names, what is particularly important in case of operator functions and templates, where is can be especially troublesome. namespace Chrono { class Date { /*... */ }; bool operator==(const Date&, const std: :string&) ; std::string format(const Date&) ; // make string representation //... } void f(Chrono::Date d, int i) { std::string s = format(d) ; // Chrono::format() std::string t = format(i) ; // error: no format() in scope }

8 Searching for Names If a function takes arguments from more than one namespace, functions are searched for in the namespaces of every argument an the function overloading is resolved in an usual way. If a function is called by a member function, the member functions of the same class and its base classes have the priority over the functions found using the argument types. namespace Chrono { class Date { /*... */ }; bool operator==(const Date&, const std: :string&); std::string format(const Date&) ; // make string representation //... } void f(Chrono::Date d, std: :string s) { if (d == s) { //... } else if (d == "August 4, 1914") { //... }

9 Namespace Aliases If the users are giving short names to the namespaces, they can cause the naming conflict Long names are inconvenient to use This dilemma can be solved by using the short alias for the long namespace namespace A { // short name, will clash (eventually) //... } A::String s1 = "Grieg"; A::String s2 = "Nielsen"; namespace American_Telephone_and_Telegraph{ // too long //... } American_Telephone_and_Telegraph::String s3 = "Grieg"; American_Telephone_and_Telegraph::String s4 = "Nielsen"; // use namespace alias to shorten names: namespace ATT = American_Telephone_and_Telegraph; ATT::String s3 = "Grieg"; ATT::String s4 = "Nielsen";

10 Namespace Aliases Thanks to the namespace aliases, the user can also refer to the library functions. It simplifies replacement of one library with another. Using Lib instead of Foundation_library_v2r11 simpifies the transition to version v3r5 - it is enough to change the initialization of Lib alias and recompile the code. Overuse of namespace aliases can lead to confusion. namespace Lib = Foundation_library_v2r11; //... Lib::set s; Lib::String s5 = "Sibelius";

11 Composition of Namespaces Frequently we want to create an interface based on existing interfaces, e.g.: Now we can use My_lib during program development: namespace His_string { class String { /*... */ }; String operator+(const String&, const String&) ; String operator+(const String&, const char*) ; void fill(char) ; //... } namespace Her_vector { template class Vector { /*... */ }; //... } namespace My_lib { using namespace His_string; using namespace Her_vector; void my_fct(String&) ; } void f() { My_lib::String s = "Byron"; // finds My_lib::His_string::String //... } using namespace My_lib; void g(Vector & vs) { //... my_fct(vs[5]) ; //... }

12 Composition of Namespaces Defining the function or data, we need to know the real name of an entity: The ideal namespace should: express the logically coherent set of features do not give the users access to unrelated features do not burden the user with a complex notation void My_lib::fill() // error: no fill() declared in My_lib { //... } void His_string::fill() // ok: fill() declared in His_string { //... } void My_lib::my_fct(My_lib::Vector & v) // ok { //... }

13 Selection Ocasionally, we want access to only a few names from a namespace. We could do that by writing a namespace declaration containing only names we want If we are not a designer of His_string namespace, this can easily get messy. A change in the "real" definition of His_string will not be reflected in this declaration. Selection of features from the namespace is more explicitly made with using declaration. A using declaration brings every declaration with a given name into scope. In particular, a single using declaration can bring in every variant of an overloaded function. namespace His_string{ // part of His_string only class String { /*... */ }; String operator+(const String&, const String&) ; String operator+(const String&, const char*) ; } namespace My_string { using His_string::String; using His_string::operator+; // use any + from His_string }

14 Composition and Selection Combining composition (by using directives) with selection (by using declarations) yields the flexibility needed for most realworld examples. With these mechanisms, we can provide access to a variety of facilities in such a way that we resolve name clashes and ambiguities arising from their composition. namespace His_lib { class String { /*... */ }; template class Vector { /*... */ }; //... } namespace Her_lib { template class Vector { /*... */ }; class String { /*... */ }; //... } namespace My_ lib { using namespace His_lib; // everything from His_lib using namespace Her_lib; // everything from Her_lib using His_lib::String; // resolve potential clash in favor of His_lib using Her_lib::Vector; // resolve potential clash in favor of Her_lib template class List { /*... */ }; // additional stuff //... }

15 Composition and Selection Names explicitly declared in a namespace (including names declared by using declarations) take priority over names made accessible in another scope by a using directive. The name in a new namespace can be changed using the typedef directive or inheritance. namespace Lib2 { using namespace His_lib; // everything from His_lib using namespace Her_lib; // everything from Her_lib using His_lib::String; // resolve potential clash in favor of His_lib using Her_lib::Vector; // resolve potential clash in favor of Her_lib typedef Her_lib::String Her_string; // rename template class His_vec // "rename" : public His_lib::Vector { /*... */ }; template class List { /*... */ }; // additional stuff //... }

16 Namespaces and Overloading Overloading works across namespaces. This is essential to allow us to migrate existing libraries to use namespaces with minimal source code changes. // old A.h: void f(int) ; //... // old B.h: void f(char) ; //... // old user.c: #include "A.h" #include "B.h" void g() { f('a') ; // calls the f() from B.h } // new A.h: namespace A { void f(int) ; //... } // new B.h: namespace B { void f(char) ; //... } // new user.c: #include "A.h" #include "B.h" using namespace A; using namespace B; void g() { f('a') ; // calls the f()from B.h }

17 Namespaces Are Open A namespace is open; that is, you can add names to it from several namespace declarations. In this way, we can support large program fragments within a single namespace. This openness is an aid in a program transition into using namespaces namespace A { int f() ; // now A has member f() } namespace A { int g() ; // now A has two members, f() and g() } // my header: void f() ; // my function //... #include int g() ; // my function //... // my header: namespace Mine { void f() ; // my function //... } #include namespace Mine { int g() ; // my function //... }

18 Namespaces Are Open Defining a previously declared entity of a namespace, it is safer to use the scope resolution operator :: using the syntax Mine:: rather than re-open the namespace Mine The compiler will not detect this error when trying to redefine ff() in the reopened namespace namespace Mine { void f(); } void Mine::ff() // error: no ff() declared in Mine { //... } namespace Mine { // reopening Mine to define functions void ff() // oops! no ff() declared in Mine; // ff() is added to Mine by this definition { //... } //... }

19 Namespaces and C Declaration of entities defined in files compiled by a C compiler need to be preceded in C++ by a declarator extern "C" C++ defines the set of standard headers, where the C library functions are available in the std namespace // cstdio namespace std { //... extern "C" int printf(const char*... ); //... } extern "C" int printf(const char*... );extern "C" {... int printf(const char*... );... }; #include //... std::printf("Hello, world\n"); //...