Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creation and Use of Namespaces

Similar presentations


Presentation on theme: "Creation and Use of Namespaces"— Presentation transcript:

1 Creation and Use of Namespaces

2 Your First Namespace // syntax namespace your_namespace_name { code elements to be put in that namespace } // example namespace price double sqrt(const double);

3 Your First Namespace // syntax namespace your_namespace_name { code elements to be put in that namespace } // example namespace price double sqrt(const double);

4 Your First Namespace // syntax namespace your_namespace_name { code elements to be put in that namespace } // example namespace price double sqrt(const double);

5 Your First Namespace // syntax namespace your_namespace_name { code elements to be put in that namespace } // example namespace price double sqrt(const double);

6 Scoping namespace price { double sqrt(const double); }

7 Scoping namespace price { double sqrt(const double); } option 1
double d = price::sqrt(9);

8 Scoping namespace price { double sqrt(const double); } option 1
double d = price::sqrt(9); using price::sqrt; ... double d = sqrt(9);

9 Scoping namespace price { double sqrt(const double); } option 1
double d = price::sqrt(9); using price::sqrt; ... double d = sqrt(9); using namespace price; ... double d = sqrt(9);

10 Preventing Conflicts namespace price { double sqrt(const double); }
option 1 double d = price::sqrt(9); double s = std::sqrt(9);

11 Preventing Conflicts namespace price { double sqrt(const double); }
option 1 option 2 double d = price::sqrt(9); double s = std::sqrt(16); using price::sqrt; using std::sqrt; ... double d = sqrt(9); double s = sqrt(16);

12 Preventing Conflicts namespace price { double sqrt(const double); }
option 1 option 2 double d = price::sqrt(9); double s = std::sqrt(16); using price::sqrt; using std::sqrt; ... double d = sqrt(9); double s = sqrt(16);

13 Preventing Conflicts namespace price { double sqrt(const double); }
option 1 option 2 option 3 double d = price::sqrt(9); double s = std::sqrt(16); using price::sqrt; using std::sqrt; ... double d = sqrt(9); double s = sqrt(16); using namespace price; using namespace std; ... double d = sqrt(9); double s = sqrt(16);

14 Preventing Conflicts namespace price { double sqrt(const double); }
option 1 option 2 option 3 double d = price::sqrt(9); double s = std::sqrt(16); using price::sqrt; using std::sqrt; ... double d = sqrt(9); double s = sqrt(16); using namespace price; using namespace std; ... double d = sqrt(9); double s = sqrt(16);

15 Open Scoping jose.h alicia.h main.cpp #ifndef JOSE_H #define JOSE_H
namespace price { int jose; } #endif #ifndef ALICIA_H #define ALICIA_H namespace price { char alicia; } #endif #include “jose.h” #include “alicia.h” using namespace price; int main() { jose = 16; alicia = ‘a’; ...

16 Open Scoping array.h array.hpp main.cpp #ifndef ARRAY_H
#define ARRAY_H #include <iostream> using std::cout; using std::endl; namespace price { template <typename t> class array public: t m_data[20]; void print(); }; } #include "array.hpp“ #endif namespace price { template <typename t> void array<t>::print() int i; for(i = 0; i < 20; i++) cout << m_data[i]; cout << ", "; } cout << endl; #include "array.h“ using price::array; int main() { array<int> a; a.m_data[0] = 24; a.print(); return 0; }

17 Open Scoping array.h array.hpp #ifndef ARRAY_H #define ARRAY_H
#include <iostream> using std::cout; using std::endl; namespace price { template <typename t> class array public: t m_data[20]; void print(); }; } #include "array.hpp“ #endif template <typename t> void price::array<t>::print() { int i; for(i = 0; i < 20; i++) cout << m_data[i]; cout << ", "; } cout << endl;

18 The Unnamed Namespace array.h array.hpp main.cpp #ifndef ARRAY_H
#define ARRAY_H #include <iostream> using std::cout; using std::endl; template <typename t> class array { public: t m_data[20]; void print(); }; #include "array.hpp“ #endif template <typename t> void array<t>::print() { int i; for(i = 0; i < 20; i++) cout << m_data[i]; cout << ", "; } cout << endl; #include "array.h“ int main() { array<int> a; a.m_data[0] = 24; a.print(); return 0; }

19 End of Session


Download ppt "Creation and Use of Namespaces"

Similar presentations


Ads by Google