Namespaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Similar documents
Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Understanding main() function Input/Output Streams

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

4. Structure of a C++ program

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CMSC 132: Object-Oriented Programming II

A A B U n i v e r s i t y

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

EL2310 Scientific Programming

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Ch. 10: Name Control

OBJECT ORIENTED PROGRAMMING USING C++

Data Structures using OOP C++ Lecture 3

10. Functions (Part 2)

Programmazione. Prof. Marco Bertini

C++ Scope Resolution Operator ::

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Compiler construction 2009

Constants, References

Your first C++ program

Programming 2. Object Oriented Programming. Daniel POP

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

CSC 330 Object Oriented Design. Namespaces

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Introduction. namespaces Program has identifiers in different scopes Sometimes scopes overlap, lead to problems. Accessing the Variables.

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

7. C++ Class and Object

EL2310 Scientific Programming

Programming II Lecture 2 Structures and Classes

Fundamentals of Programming Session 25

Chapter-13 USER DEFINED FUNCTIONS

Module 7 b. -Namespaces -Exceptions handling

Global & Local Identifiers

Object-Oriented Programming (OOP) Fundamental Principles of OOP

C and C++ I. Spring 2014 Carola Wenk

Exceptions, Case Study-Exception handling in C++.

Chapter 8 - Notes User-Defined Simple Data Types, Namespaces, and the string Type

Chapter Two MULTIPLE CHOICE

A Fast Review of C Essentials Part II

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

UNIT 3 FUNCTIONS AND ARRAYS

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Computing and Statistical Data Analysis Lecture 3

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

CSI33 Data Structures

Reminder the scope of a variable is the part of the program where that variable is visible Will this compile?

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Introduction To C#.NET

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

by Pearson Education, Inc. All Rights Reserved. 2

EL2310 Scientific Programming

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Computer Programming : C++

Fundamentals of Programming Session 23

Lecture 2 More C++ Jon Macey

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

2 nd Week Lecture Notes

Developed By Strawberry


Programming I Test 2 Version 1 CS 1613 Summer 2011

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Protection Levels and Constructors The 'const' Keyword

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Fundamentals of Programming Session 12

UEE1302 (1102) F10: Introduction to Computers and Programming

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Data Structures Unit 02

Separate Compilation of Multi-File Programs

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Fundamental Concepts and Definitions

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Scientific Computing

Cpt S 122 Data Structures. Introduction to C++ Part II

Array. 9 January 2015 OSU CSE 1

Fundamentals of Programming Session 24

COMP322 - Introduction to C++

LAMBDA EXPRESSIONS. Summer 2018

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

C How to Program, 6/e, 7/e

Transcription:

Namespaces OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani

using namespace std; We have been using the above statement in our programs. The using namespace statement specifies that the members defined in std namespace will be used frequently throughout the program.

Consider a situation, when we have two persons with the same name, Pooja, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like surname, area of living, father s name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

A1.h class A //body of A ; A2.h class A //body of A ; Main_prog.cpp #include A1.h #include A2.h main() A Aobj; //ERROR: Ambiguity error due to multiple definitions of A

How to overcome such problems? A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. In essence, a namespace defines a scope. Namespaces enable the C++ programmer to prevent pollution of the global namespace that leads to name clashes.

Defining a Namespace: namespace namespace_name //declarations of variables, functions, classes, etc. Difference between a class definition and namespace definition is, namespace is concluded with a closing brace but no terminating semicolon.

Ex: namespace TestSpace int m; void display(int n) cout<<n; //no semicolon here Here, the variable m and the function display() are inside the scope defined by TestSpace namespace. To assign value to m, use :: operator. TestSpace::m = 100; To call a function display() TestSpace::display(10); EX: NameSpace1.cpp

A1.h namespace A1 class A //body of A ; A2.h namespace A2 class A //body of A ; Main_prog.cpp #include A1.h #include A2.h main() A1::A Aobj1; // OK: Aobj1 is an object of the class defined in A1.h A2::A Aobj2; // OK: Aobj2 is an object of the class defined in A2.h

This approach becomes cumbersome if the members of a namespace are frequently used. In such cases, we can use using directive to simplify the access. 2 ways: using namespace namespace_name; //using directive using namespace_name :: member_name; //using declaration

Ex: using namespace TestSpace; m = 100; //OK display(200); //OK using TestSpace :: m; m = 100; //OK display(200); //NOT OK, display not visible Main_prog.cpp NOTE: the using directive brings back the #include A1.h #include A2.h global namespace pollution that the main() namespaces mechanism was supposed to using namespace A1; remove. A Aobj1; // OK: Aobj1 is an object of the class defined in A1.h using namespace A2; A Aobj2;

Some namespaces have long names. Qualifying the name of a class that is enclosed within such a namespace, with the name of the namespace, is cumbersome. Ex: namespace a_very_very_long_name class A - - - - - ; void main() a_very_very_long_name :: A A1;

Assigning a suitably short alias to such a long namespace solves the problem. namespace a_very_very_long_name class A ; - - - - - namespace x = a_very_very_long_name void main() x :: A A1;

Aliases provides benefits also. Suppose an alias has been used at a number of places in the source code. Ex: Suppose alias X refers to a namespace N1 namespace X = N1; //declaring an alias Suppose that this alias has been used extensively in the source code. X :: A obj; //obj is an object of class A that is enclosed in namespace N1 obj.f1(); //f1() is a member function of the above class If the declaration of alias X is modified as follows namespace X = N2; //modifying the alias, N2 is also a namespace Then all the references made by X would now refer to namespace N2.

Point to be noted: Such a statement would compile only if both namespaces, N1 and N2, contain a class named A, and if these two classes have the same interface.

References Sourav Sahay, Objected Oriented Programming with C++ E Balagurusamy, Objected Oriented Programming with C++