C++ Scope Resolution Operator ::

Similar documents
10. Functions (Part 2)

Lecture 14. Xiaoguang Wang. March 11th, 2014 STAT 598W. (STAT 598W) Lecture 14 1 / 36

Unit 1 : Principles of object oriented programming

W3101: Programming Languages C++ Ramana Isukapalli

Lab Instructor : Jean Lai

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Lecture on pointers, references, and arrays and vectors

UNIT V FILE HANDLING

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

Object Oriented Design

CMSC 202 Midterm Exam 1 Fall 2015

4. Structure of a C++ program

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

Inheritance, and Polymorphism.

VARIABLES & ASSIGNMENTS

2.9 DCL58-CPP. Do not modify the standard namespaces

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

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

Functions and Recursion

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

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

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

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

Programming Language. Functions. Eng. Anis Nazer First Semester

Introduction Of Classes ( OOPS )

OBJECT ORIENTED PROGRAMMING USING C++

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

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

FROM C TO C++ CRASH COURSE FOR C PROGRAMMERS -PART I- APPLAB

BITG 1113: Function (Part 2) LECTURE 5

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Functions, Arrays & Structs

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Type Conversion. and. Statements

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

AN OVERVIEW OF C++ 1

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

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

DELHI PUBLIC SCHOOL TAPI

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

Computer Programming

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Dr. Md. Humayun Kabir CSE Department, BUET

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

C++ Namespaces, Exceptions

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

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Exercise 1.1 Hello world

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

1. (25 pts) Short Answer. Provide brief (1-3 sentence) answers to the following:

Pointers and References

1 Unit 8 'for' Loops

Object Oriented Design

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

OpenMP Algoritmi e Calcolo Parallelo. Daniele Loiacono

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

2 is type double 3 auto i = 1 + 2; // evaluates to an 4 integer, so it is int. 1 auto d = 5.0; // 5.0 is a double literal, so it

CMPS 221 Sample Final

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

2 nd Week Lecture Notes

Variables. Data Types.

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

CSC1322 Object-Oriented Programming Concepts

a. a * c - 10 = b. a % b + (a * d) + 7 =

CSC 211 Intermediate Programming. Arrays & Pointers

Programming in C++: Assignment Week 8

C++ Programming for Non-C Programmers. Supplement

PIC 10A Objects/Classes

2. Functions I: Passing by Value

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Lecture 23: Pointer Arithmetic

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Chapter 17 - Notes Recursion

Increment and the While. Class 15

Absolute C++ Walter Savitch

C++ Programming for Non-C Programmers. Supplement

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

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

Object Reference and Memory Allocation. Questions:

COMP322 - Introduction to C++ Lecture 07 - Introduction to C++ classes

Object-Oriented Programming Concepts

TEMPLATE IN C++ Function Templates

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

CS24 Week 3 Lecture 1

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

IBM i Version 7.2. Programming IBM Rational Development Studio for i ILE C/C++ Language Reference IBM SC

Array Elements as Function Parameters

C++ basics Getting started with, and Data Types.

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Input And Output of C++

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

Transcription:

C++ Scope Resolution Operator :: C++The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example: int count = 0; int main(void) int count = 0; ::count = 1; // set global count to 1 count = 2; // set local count to 2 The declaration of count declared in the main() function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope. You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator. In the following example, the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator. class X public: static int count; ; int X::count = 10; member // define static data

int main () int X = 0; of class X cout << X::count << endl; // hides class type X // use static member Namespaces Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. The format of namespaces is: namespace identifier entities Where identifier is any valid identifier and entities is the set of classes, objects and functions that are included within the namespace. For example: namespace mynamespace int a, b; In this case, the variables a and b are normal variables declared within a namespace called mynamespace. In order to access these variables from outside the mynamespace namespace we have to use the scope operator ::. For example, to access the previous variables from outside [mynamespace=] we can write: general::a general::b The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors. For example:

// namespaces namespace first int var = ; double var = 3.1416; int main () cout << first::var << endl; cout << second::var << endl; 3.1416 In this case, there are two global variables with the same name: var. One is defined as an int within the namespace first and the other one in defined as a double in the namespace called second. No redefinition errors happen thanks to namespaces. The 'using' Keyword The keyword using is used to introduce a name from a namespace into the current declarative region. For example: // using example namespace first

int x = ; int y = 10; double x = 3.1416; double y = 2.7183; int main () using first::x; using second::y cout << y << endl; cout << first::y << endl; cout << second::x << endl; 2.7183 10 2.7183 Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names. The keyword using can also be used as a directive to introduce an entire namespace: // using example namespace first

int x = ; int y = 10; double x = 3.1416; double y = 2.7183; int main () using namespace first; cout << y << endl; cout << second::y << endl; cout << second::x << endl; 10 3.1416 2.7183 In this case, since we have declared that we were using namespace first, all direct uses of x and y without name qualifiers were referring to their declarations in namespace first. using and using namespace have validity only in the same block in which they are stated or in the entire code if they are used directly in the global scope. For example, if we had the intention to first use the objects of one namespace and then those of another one, we could do something like: // using namespace example

namespace first int x = ; double x = 3.1416; int main () using namespace first; using ; 3.1416 Namespace alias We can declare alternate names for existing namespaces according to the following format: namespace new_name = current_name; Namespace std All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.

Unnamed Namespaces An unnamed-namespace-definition behaves as if it were replaced by: namespace unique namespace-body using namespace unique; Each unnamed namespace has an identifier, represented by unique, that differs from all other identifiers in the entire program. For example: namespace int i; void f() i++; namespace A namespace int i; int j; // A::unique::i // A::unique::j // unique::i // unique::i++ using namespace A; void h() i++; A::i++; j++; //error: unique::i or A::unique::i // error: A::i undefined // A::unique::j++ Unnamed namespaces are a superior replacement for the static declaration of variables. They allow variables and functions to be visible within an entire translation unit, yet not visible externally. Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.