Object Oriented Programming Using C++: C++ Namespaces.pptx
RashidFaridChishti
13 views
11 slides
May 18, 2024
Slide 1 of 11
1
2
3
4
5
6
7
8
9
10
11
About This Presentation
Object Oriented Programming Using C++: C++ Namespaces
Size: 431.15 KB
Language: en
Added: May 18, 2024
Slides: 11 pages
Slide Content
Object Oriented Programming Namespace Engr. Rashid Farid Chishti [email protected] https://youtube.com/rfchishti https://sites.google.com/site/chishti International Islamic University H-10, Islamabad, Pakistan http://www.iiu.edu.pk 1 Video Lecture
Consider a situation, when we have two persons with the same name, M. Ali , in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either Reg. Number or their Father’s Name , etc. Same situation can arise in your C++ program. For example, you might be writing some code that has a function called print() and there is another library available which is also having same function print() . Now the compiler has no way of knowing which version of print() function you are referring to within your code. A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the Introduction
same name available in different libraries . Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope . A namespace definition begins with the keyword namespace followed by the namespace name. To call the namespace-enabled version of either function or variable, write scope resolution operator (::) between the namespace and the code: Introduction namespace namespace_name { // code declarations } namespace_name ::code
#include < iostream > // Define a namespace called " MyNamespace " namespace MyNamespace { // Define a simple function within the namespace void displayMessage () { std :: cout << "Hello from MyNamespace !" << std :: endl ; } } int main() { // Call the function from the namespace without using the namespace prefix MyNamespace :: displayMessage (); return 0; } Example 1: Using namespace
#include < iostream > #include < stdlib.h > namespace Cartesian_Coordinates { double x = 11.2; double y = 8.7; void Show(){ std :: cout << "x = " << x << ", y = " << y << std :: endl ; } } Example 2: Using tw o namespaces namespace Spherical_Coordinates { double r = 14.18; double theta = 37.84; void Show(){ std :: cout << "r = " << r << ", Theta = " << theta << std :: endl ; } } int main() { Cartesian_Coordinates ::Show(); Spherical_Coordinates ::Show(); system( "pause" ); return 0; } Page 1 Page 2
#include < iostream > #include < stdlib.h > namespace Cartesian_Coordinates { double x = 11.2; double y = 8.7; void Show(){ std :: cout << "x = " << x << ", y = " << y << std :: endl ; } } Example 3: The using directive namespace Spherical_Coordinates { double r = 14.18; double theta = 37.84; void Show(){ std :: cout << "r = " << r << ", Theta = " << theta << std :: endl ; } } using namespace Spherical_Coordinates ; int main() { Show(); // it will call Show from // Spherical_Coordinates namespace system ( "pause" ); return 0; } Page 1 Page 2
#include < iostream > // Define a namespace called "Shapes" namespace Shapes { class Circle { private : double radius; public : Circle( double radius ):radius(radius ) {} double calculateArea () const { return 3.14 * radius * radius; } }; } Example 4: Using class in a namespace int main() { // Create an instance of the Circle class // from the Shapes namespace Shapes ::Circle myCircle (5.0); std :: cout << "Area of the circle: " << myCircle.calculateArea () << std :: endl ; return 0; } Page 1 Page 2
#include < iostream > using namespace std ; namespace Geometry { // Define a namespace called "Geometry" class Point { // Define a class called "Point" to represent a point in 2D space public : int x,y ; Point( int x, int y) : x(x), y(y) {} void Show() const { cout << "(" << x << ", " << y << ")" ; } }; class Rectangle { private : Point topLeft , bottomRight ; public : Rectangle(Point TL, Point BR): topLeft (TL), bottomRight (BR) {} void Draw() const { int height = bottomRight.x - topLeft.x ; int width = bottomRight.y - topLeft.y ; Example 5: Using two classes in a namespace Page 1
for ( int row = 0 ; row < height ; row++){ for ( int col = 0 ; col< width ; col++){ cout << "*" ; } cout << "\n" ; } } }; } int main() { // Create instances of classes from the Geometry namespace Geometry ::Point topLeft (5, 7 ); Geometry ::Point bottomRight (8, 11); cout << "Top Left = " ; topLeft.Show (); cout << "\n" ; cout << "Bottom Right = " ; bottomRight.Show (); cout << "\n" ; Geometry ::Rectangle rectangle( topLeft , bottomRight ); rectangle.Draw (); return 0; } Example 5: Using two classes in a namespace Page 2
Namespaces can be nested where you can define one namespace inside another name space. You can access members of nested namespace by using resolution operators. Nested Namespaces namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } } // to access members of namespace_name2 using namespace namespace_name1::namespace_name2; // to access members of namespace:name1 using namespace namespace_name1;
#include < iostream > #include < stdlib.h > using namespace std ; // first name space namespace first_space { void func () { cout << "Inside first_space " << endl ; } // second name space namespace second_space { void func () { cout << "Inside second_space " << endl ; } } } Example 6 : Nested Namespaces using namespace first_space :: second_space ; int main () { // This will call function from // second name space. func (); system ( "PAUSE" ); return 0; } // now try with // using namespace first_space Page 1 Page 2