Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with ANSI C ++

Similar presentations


Presentation on theme: "Programming with ANSI C ++"— Presentation transcript:

1 Programming with ANSI C ++
A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

2 Chapter 14 Namespaces

3 Introduction The Name Conflict problem Global namespace
Difference between previous and current versions of C++ Need for logical grouping Fully qualified names and scope resolution operator

4 Using Namespaces Using declaration Using directive
using namespace std; Using directive using std::operator <<; Fully qualified name std::cout << “Hi”

5 User Defined Namespace
namespace MyNamespace { int MyNumber; string MyString; } int main() cin >> MyNamespace::MyNumber; …. }

6 Applying using to MyNamespace
Using declaration using MyNamespace::MyNumber; using MyNamespace::MyString; Using directive using namespace MyNamespace; cin >>MyNumber; cin >>MyString;

7 Defining functions inside namespace
namespace MyNamespace { int MyNumber; string MyString; void InputAndDisplay() { // Reading my number and string and display them }

8 Defining class inside namespace
namespace MyNamespace { class brother { …. } int main() { MyNamespace::brother Lara ("BrianLara"); cout << Lara; }

9 Defining outside the namespace
void MyNamespace::InputAndDisplay()/* qualifying the function name and defining it outside */ MyNamespace::brother::brother(string BrotherName) // constructor for brother class defined inside namespace MyNamespace

10 Extending the namespace
We can define namespace in parts. Each part adds to previous definition. New definition do not overwrite older one

11 Unnamed Namespaces Content declared and defined in unnamed namespace is Available in any module that is part of the same physical file not available to other modules available at the time of linking Better replacement for global static variables Useful in access declarations

12 Nested namespaces A namespace can be defined inside another namespace
namespace OuterNamespace { namespace InnerNamespace { …… }; }; OuterNamespace::InnerNamespace::brother Lara("BrianLara");

13 Namespace aliases An alias can be defined for a longer namespace name and used like below namespace NS = OuterNamespace::InnerNamespace; NS::brother Lara("BrianCharlesLara"); Namespaces can also be used to provide dynamism when more than one software versions are to be addressed

14 The std Namespace It is a namespace where the standard library routines are defined Insertion into global namespace by using statement <iostream> and <cstdlib> instead of <iostream.h> and <stdio.h> are using the std namespace std namespace can not be extended

15 The Koenig lookup A smart compiler guessing the namespace by context
Andrew Koenig’s ‘argument dependent lookup’ algorithm It is standard now

16 No Overhead The namespaces are resolved at compile time.
All using directives, declarations, fully qualified names are taken care of statically by the compiler. There is no run time overhead with namespaces.


Download ppt "Programming with ANSI C ++"

Similar presentations


Ads by Google