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

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

Module 7 b. -Namespaces -Exceptions handling

4. Structure of a C++ program

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

Global & Local Identifiers

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

Understanding main() function Input/Output Streams

7. C++ Class and Object

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Protection Levels and Constructors The 'const' Keyword

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

10. Functions (Part 2)

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

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

Lecture 5 Tao Wang 1

W3101: Programming Languages C++ Ramana Isukapalli

Data Structures using OOP C++ Lecture 3

CSI33 Data Structures

Constants, References

EL2310 Scientific Programming

Lecture 7. Log into Linux New documents posted to course webpage

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter-14 STRUCTURES

CMSC 202 Midterm Exam 1 Fall 2015

BTE2313. Chapter 2: Introduction to C++ Programming


Array Elements as Function Parameters

EL2310 Scientific Programming

OBJECT ORIENTED PROGRAMMING USING C++

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

Typescript on LLVM Language Reference Manual

Your First C++ Program. September 1, 2010

C++ For Science and Engineering Lecture 15

G52CPP C++ Programming Lecture 13

Classes and Objects in C++

In this chapter you will learn:

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

Inheritance, and Polymorphism.

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

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

Ch. 10: Name Control

C++ Scope Resolution Operator ::

Implementing Abstract Data Types (ADT) using Classes

Introduction to Programming Using Java (98-388)

Lab Instructor : Jean Lai

Lecture 18 Tao Wang 1

C++ For Science and Engineering Lecture 12

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES III

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Tokens, Expressions and Control Structures

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

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

A Fast Review of C Essentials Part II

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

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Variables. Data Types.

Introduction To C#.NET

Data Types and Variables in C language

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

a data type is Types

Chapter 7 - Notes User-Defined Functions II

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

FRIEND FUNCTIONS IN CPP

Your first C++ program

2. Functions I: Passing by Value

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Friends and Overloaded Operators

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

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

Friends and Overloaded Operators

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

PIC 10A Objects/Classes

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

04-19 Discussion Notes

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Lecture 3 Tao Wang 1

Chapter 16. Templates. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Composition I. composition is a way to combine or compose multiple classes together to create new class

2.8. Decision Making: Equality and Relational Operators

CSCI 1370 APRIL 26, 2017

CLASSES AND OBJECT CHAPTER 04 CLASS XII

C++ Programming Lecture 1 Software Engineering Group

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Objects and Classes Lecture 2

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

1 Unit 8 'for' Loops

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Recharge (int, int, int); //constructor declared void disply();

LECTURE 02 INTRODUCTION TO C++

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Transcription:

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14) Some Material for your reference: Consider following C++ program. // A program to demonstrate need of namespace int value; value = 0; double value; // Error here value = 0.0; Output : Compiler Error: 'value' has a previous declaration as 'int value' In each scope, a name can only represent one entity. So, there cannot be two variables with the same name in the same scope. Using namespaces, we can create two variables or member functions having the same name. // Here we can see that more than one variables // are being used without reporting any error. // That is because they are declared in the // different namespaces and scopes. // Variable created inside namespace namespace first int val = 500; // Global variable int val = 100;

// Local variable int val = 200; // These variables can be accessed from // outside the namespace using the scope // operator :: cout << first::val << '\n'; 500 Definition and Creation: Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names. Namespace is a feature added in C++ and not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it. Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope. A namespace definition begins with the keyword namespace followed by the namespace name as follows: namespace namespace_name int x, y; // code declarations where // x and y are declared in // namespace_name's scope Namespace declarations appear only at global scope. Namespace declarations can be nested within another namespace. Namespace declarations don t have access specifiers. (Public or private) No need to give semicolon after the closing brace of definition of namespace. We can split the definition of namespace over several units. // Creating namespaces 1 int value() return 5;

2 const double x = 100; double value() return 2*x; // Access value function within ns1 cout << ns1::value() << '\n'; // Access value function within ns2 cout << ns2::value() << '\n'; // Access variable x directly cout << ns2::x << '\n'; 5 200 100 Classes and Namespace: Following is a simple way to create classes in a name space // A C++ program to demonstrate use of class // in a namespace // A Class in a namespace class geek void display() ;

// Creating Object of student Class ns::geek::display() Class can also be declared inside namespace and defined outside namespace using following syntax // A C++ program to demonstrate use of class // in a namespace // Only declaring class here class geek; // Defining class outside class ns::geek void display() ; //Creating Object of student Class

ns::geek::display() We can define methods also outside the namespace. Following is an example code. // A C++ code to demonstrate that we can define // methods outside namespace. // Creating a namespace void display(); class geek void display(); ; // Defining methods of namespace void ns::geek::display() void ns::display() cout << "ns::display()\n"; // Driver code ns::display(); ns::display() ns::geek::display()