SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
1. C++ Scope Resolution Operator:: 
2. Pointers 
C++ Scope Resolution Operator:: 
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=% 
2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm 
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 
return 0; 
} 
 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. 
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. 
#include <iostream> 
using namespace std; 
class X 
{ 
public: 
static int count; 
}; 
int X::count = 10;// define static data member 
int main () 
{ 
int X = 0; // hides class type X 
// use static member of class X 
cout << X::count << endl; 
}
C++ Scope Resolution Operator :: 
PURPOSE 
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. 
Namespaces 
Namespaces allow to group entities like classes, 
objects and functions under a name. 
In 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 
#include <iostream> 
using namespace std; 
namespace first 
{ 
int var = 5; 
} 
namespace second 
{ 
double var = 3.1416; 
} 
int main () { 
cout << first::var << endl; 
cout << second::var << endl; 
return 0; 
} 
EXAMPLE OUTPUT: 
5 
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 
#include <iostream> 
using namespace std; 
namespace first 
{ 
int x = 5; 
int y = 10; 
} 
namespace second 
{ 
double x = 3.1416; 
double y = 2.7183; 
} 
int main () { 
using first::x; 
using second::y 
cout << x << endl; 
cout << y << endl; 
cout << first::y << endl; 
cout << second::x << endl; 
return 0; 
} 
EXAMPLE OUTPUT: 
5 
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 
#include <iostream> 
using namespace std; 
namespace first 
{ 
int x = 5; 
int y = 10; 
} 
namespace second 
{ 
double x = 3.1416; 
double y = 2.7183; 
} 
int main () { 
using namespace first; 
cout << x << endl; 
cout << y << endl; 
cout << second::y << endl; 
cout << second::x << endl; 
return 0; 
}
EXAMPLE OUTPUT: 
5 
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 
#include <iostream> 
using namespace std; 
namespace first 
{ 
int x = 5; 
} 
namespace second 
{ 
double x = 3.1416; 
} 
int main () 
{ 
// Here are two blocks 
{ 
using namespace first; 
cout << x << endl; 
} 
{ 
using namespace second; 
cout << x << endl; 
} 
return 0; 
} 
EXAMPLE OUTPUT: 
5 
3.1416
What Are Pointers? 
Reference: 
http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm 
 C++ pointers are easy and fun to learn. 
 Some C++ tasks are performed more easily with 
pointers, and 
 other C++ tasks, such as dynamic memory allocation, 
cannot be performed without them. 
As you know every variable is a memory location and every 
memory location has its address defined which can be 
accessed using 
ampersand (&) operator which denotes an address in 
memory. Consider the following which will print the address 
of the variables defined:#include <iostream> 
using namespace std; 
int main () 
{ 
int var1; 
char var2[10]; 
cout << "Address of var1 variable: "; 
cout << &var1 << endl; 
cout << "Address of var2 variable: "; 
cout << &var2 << endl; 
return 0; 
} 
When the above code is compiled and executed, it produces result something as follows: 
Address of var1 variable: 0xbfebd5c0 
Address of var2 variable: 0xbfebd5b6
What Are Pointers? 
 A pointer is a variable whose value is the address of 
another variable. 
 Like any variable or constant, you must declare a 
pointer before you can work with it. 
 The general form of a pointer variable declaration is: 
type *variablename; 
Here, type is the pointer's base type; it must be a valid C++ type and 
variablename is the name of the pointer variable. 
The asterisk you used to declare a pointer is the same asterisk that you use for 
multiplication. 
However, in this statement the asterisk is being used to designate a variable as 
a pointer. Following are the valid pointer declaration: 
int *ip; // pointer to an integer 
double *dp; // pointer to a double 
float *fp; // pointer to a float 
char *ch // pointer to character 
The actual data type of the value of all pointers, whether 
integer, float, character, or otherwise, is the same, a long 
hexadecimal number that represents a memory address. The 
only difference between pointers of different data types is 
the data type of the variable or constant that the pointer 
points to.
There are few important operations Using Pointers in C++: 
Which we will do with the pointers very frequently: 
(a) We define pointer variables 
(b) Assign the address of a variable to a pointer and 
(c) Finally access the value at the address available in the pointer variable. 
This is done by using unary operator * that returns the value of the variable 
located at the address specified by its operand. Following example makes use 
of these operations: 
#include <iostream> 
using namespace std; 
int main () 
{ 
int var = 20; // actual variable declaration. 
int *ip; // pointer variable 
ip = &var; // store address of var in pointer variable 
cout << "Value of var variable: "; 
cout << var << endl; 
// print the address stored in ip pointer variable 
cout << "Address stored in ip variable: "; 
cout << ip << endl; 
// access the value at the address available in pointer 
cout << "Value of *ip variable: "; 
cout << *ip << endl; 
return 0; 
}
When the above code is compiled and executed, it produces 
result something as follows: 
Value of var variable: 20 
Address stored in ip variable: 0xbfc601ac 
Value of *ip variable: 20 
C++ Pointers in Detail: 
Pointers have many but easy concepts and they are very 
important to C++ programming. There are following 
few important pointer concepts which should be clear 
to a C++ programmer: 
Concept Description 
C++ Null Pointers 
C++ supports null pointer, which is a 
constant with a value of zero defined in 
several standard libraries. 
C++ pointer arithmetic 
There are four arithmetic operators 
that can be used on pointers: ++, --, +, - 
C++ pointers vs arrays 
There is a close relationship between 
pointers and arrays. Let us check how? 
C++ array of pointers 
You can define arrays to hold a number 
of pointers. 
C++ pointer to pointer 
C++ allows you to have pointer on a 
pointer and so on. 
Passing pointers to 
functions 
Passing an argument by reference or by 
address both enable the passed 
argument to be changed in the calling 
function by the called function. 
Return pointer from 
functions 
C++ allows a function to return a 
pointer to local variable, static variable 
and dynamically allocated memory as 
well.
C++ NULL Pointers 
 It is always a good practice to assign the pointer NULL to 
a pointer variable in case you do not have exact address 
to be assigned. 
 This is done at the time of variable declaration. A pointer 
that is assigned NULL is called a null pointer. 
 The NULL pointer is a constant with a value of zero 
defined in several standard libraries, including iostream. 
Consider the following program: 
#include <iostream> 
using namespace std; 
int main () 
{ 
int *ptr = NULL; 
cout << "The value of ptr is " << ptr ; 
return 0; 
} 
When the above code is compiled and executed, it 
produces following result: 
The value of ptr is 0 
To check for a null pointer you can use an if statement as follows: 
if(ptr) // succeeds if p is not null 
if(!ptr) // succeeds if p is null
C++ Pointer Arithmetics 
 As you understood pointer is an address which is a 
numeric value. Therefore, you can perform arithmetic 
operations on a pointer just as you can a numeric value. 
 There are four arithmetic operators that can be used on 
pointers: ++, --, +, and - 
ptr++ 
 the ptr will point to the location 1004 because each time 
ptr is incremented, it will point to the next integer 
. 
 This operation will move the pointer to next memory 
location without impacting actual value at the memory 
location. 
 If ptr points to a character whose address is 1000, then 
above operation will point to the location 1001 because 
next character will be available at 1001.
Incrementing a Pointer: 
 We prefer using a pointer in our program instead of an array 
because the variable pointer can be incremented, unlike the 
array name which cannot be incremented because it is a 
constant pointer. 
 The following program increments the variable pointer to 
access each succeeding element of the array: 
#include <iostream> 
using namespace std; 
const int MAX = 3; 
int main () 
{ 
int var[MAX] = {10, 100, 200}; 
int *ptr; 
// let us have array address in pointer. 
ptr = var; 
for (int i = 0; i < MAX; i++) 
{ 
cout << "Address of var[" << i << "] = "; 
cout << ptr << endl; 
cout << "Value of var[" << i << "] = "; 
cout << *ptr << endl; 
// point to the next location 
ptr++; 
} 
return 0; 
}
Pointers and arrays are strongly related. 
In fact, pointers and arrays are interchangeable in many cases. 
For example, a pointer that points to the beginning of an array 
can access that array by using either pointer arithmetic or 
array-style indexing. Consider the following program: 
#include <iostream> 
using namespace std; 
const int MAX = 3; 
int main () 
{ 
int var[MAX] = {10, 100, 200}; 
int *ptr; 
// let us have array address in pointer. 
ptr = var; 
for (int i = 0; i < MAX; i++) 
{ 
cout << "Address of var[" << i << "] = "; 
cout << ptr << endl; 
cout << "Value of var[" << i << "] = "; 
cout << *ptr << endl; 
// point to the next location 
ptr++; 
} 
return 0; 
}
When the above code is compiled and executed, it 
produces result something as follows: 
Address of var[0] = 0xbfa088b0 
Value of var[0] = 10 
Address of var[1] = 0xbfa088b4 
Value of var[1] = 100 
Address of var[2] = 0xbfa088b8 
Value of var[2] = 200 
MORE EXAMPLES 
#include <iostream> 
using namespace std; 
const int MAX = 3; 
int main () 
{ 
int var[MAX] = {10, 100, 200}; 
int *ptr[MAX]; 
for (int i = 0; i < MAX; i++) 
{ 
ptr[i] = &var[i]; // assign the address of integer. 
} 
for (int i = 0; i < MAX; i++) 
{ 
cout << "Value of var[" << i << "] = "; 
cout << *ptr[i] << endl; 
} 
return 0; 
} 
When the above code is compiled and executed, it produces following 
result: 
Value of var[0] = 10 
Value of var[1] = 100 
Value of var[2] = 200 
You can also use an array of pointers to character to store 
a list of strings as follows: 
#include <iostream>
using namespace std; 
const int MAX = 4; 
int main () 
{ 
char *names[MAX] = { 
"Zara Ali", 
"Hina Ali", 
"Nuha Ali", 
"Sara Ali", 
}; 
for (int i = 0; i < MAX; i++) 
{ 
cout << "Value of names[" << i << "] = "; 
cout << names[i] << endl; 
} 
return 0; 
} 
When the above code is compiled and executed, it produces following result: 
Value of names[0] = Zara Ali 
Value of names[1] = Hina Ali 
Value of names[2] = Nuha Ali 
Value of names[3] = Sara Ali

More Related Content

What's hot (20)

PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
PPTX
Event In JavaScript
ShahDhruv21
 
PPTX
Constructor ppt
Vinod Kumar
 
PPT
Scripting languages
teach4uin
 
PPTX
Html ppt
santosh lamba
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPTX
DBMS Keys
Tarun Maheshwari
 
PPT
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
PPTX
class and objects
Payel Guria
 
PPT
Introduction to PHP
Jussi Pohjolainen
 
PPT
Java Streams
M Vishnuvardhan Reddy
 
ODP
Android App Development - 05 Action bar
Diego Grancini
 
PPTX
Javascript operators
Mohit Rana
 
PPTX
Graph in data structure
Abrish06
 
PPTX
PHP FUNCTIONS
Zeeshan Ahmed
 
PPT
Java: GUI
Tareq Hasan
 
PPT
Abstract data types
Poojith Chowdhary
 
PPTX
Java script
Shyam Khant
 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Files in c++
Selvin Josy Bai Somu
 
Event In JavaScript
ShahDhruv21
 
Constructor ppt
Vinod Kumar
 
Scripting languages
teach4uin
 
Html ppt
santosh lamba
 
Pointers C programming
Appili Vamsi Krishna
 
DBMS Keys
Tarun Maheshwari
 
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
class and objects
Payel Guria
 
Introduction to PHP
Jussi Pohjolainen
 
Java Streams
M Vishnuvardhan Reddy
 
Android App Development - 05 Action bar
Diego Grancini
 
Javascript operators
Mohit Rana
 
Graph in data structure
Abrish06
 
PHP FUNCTIONS
Zeeshan Ahmed
 
Java: GUI
Tareq Hasan
 
Abstract data types
Poojith Chowdhary
 
Java script
Shyam Khant
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 

Viewers also liked (18)

PPT
Operators in C++
Sachin Sharma
 
PPTX
C++ io manipulation
Pedro Hugo Valencia Morales
 
PDF
Cement Industries
SAFFI Ud Din Ahmad
 
PDF
Material and energy balance
MagnusMG
 
PPTX
C++ operator
veena_bhagyawani
 
PPTX
Data members and member functions
Marlom46
 
PDF
Carbon dioxide Industries
SAFFI Ud Din Ahmad
 
PDF
Sodium carbonate Industries
SAFFI Ud Din Ahmad
 
PDF
Chemical engineering Introduction to Process Calculations Stoichiometry
Hassan Salem
 
PDF
Material and Energy Balance
MagnusMG
 
PDF
Rehabilitación de Pozos Petroleros
MagnusMG
 
PDF
Sulfuric acid Industries
SAFFI Ud Din Ahmad
 
PDF
Introduction to Chemical Engineering
SAFFI Ud Din Ahmad
 
PDF
Process design for chemical engineers
Amanda Ribeiro
 
PPTX
Concept Of C++ Data Types
k v
 
PPTX
Abstract Base Class and Polymorphism in C++
Liju Thomas
 
PPTX
Polymorphism
Kumar Gaurav
 
PDF
Polymorphism
Raffaele Doti
 
Operators in C++
Sachin Sharma
 
C++ io manipulation
Pedro Hugo Valencia Morales
 
Cement Industries
SAFFI Ud Din Ahmad
 
Material and energy balance
MagnusMG
 
C++ operator
veena_bhagyawani
 
Data members and member functions
Marlom46
 
Carbon dioxide Industries
SAFFI Ud Din Ahmad
 
Sodium carbonate Industries
SAFFI Ud Din Ahmad
 
Chemical engineering Introduction to Process Calculations Stoichiometry
Hassan Salem
 
Material and Energy Balance
MagnusMG
 
Rehabilitación de Pozos Petroleros
MagnusMG
 
Sulfuric acid Industries
SAFFI Ud Din Ahmad
 
Introduction to Chemical Engineering
SAFFI Ud Din Ahmad
 
Process design for chemical engineers
Amanda Ribeiro
 
Concept Of C++ Data Types
k v
 
Abstract Base Class and Polymorphism in C++
Liju Thomas
 
Polymorphism
Kumar Gaurav
 
Polymorphism
Raffaele Doti
 
Ad

Similar to 18 dec pointers and scope resolution operator (20)

PPT
2.overview of c++ ________lecture2
Warui Maina
 
DOCX
C++ Pointers with Examples.docx
JoeyDelaCruz22
 
DOCX
Arrry structure Stacks in data structure
lodhran-hayat
 
PPTX
Classes function overloading
ankush_kumar
 
PDF
CS225_Prelecture_Notes 2nd
Edward Chen
 
DOCX
programing fundamentals complete solutions.docx
ZAMANYousufzai1
 
PPT
Lecture 8
Mohammed Saleh
 
PPT
Bca 2nd sem u-5 files & pointers
Rai University
 
PPT
Mca 2nd sem u-5 files & pointers
Rai University
 
PDF
C++ Course - Lesson 3
Mohamed Ahmed
 
PDF
Namespace--defining same identifiers again
Ajay Chimmani
 
PPTX
Operators
moniammu
 
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Ameer Hamxa
 
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
PPTX
1. DSA - Introduction.pptx
hara69
 
DOCX
Bc0037
hayerpa
 
PPTX
Namespace in C++ Programming Language
Himanshu Choudhary
 
PPTX
Object Oriented Programming Using C++: C++ Namespaces.pptx
RashidFaridChishti
 
PPTX
C++ FUNCTIONS-1.pptx
ShashiShash2
 
PPTX
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
2.overview of c++ ________lecture2
Warui Maina
 
C++ Pointers with Examples.docx
JoeyDelaCruz22
 
Arrry structure Stacks in data structure
lodhran-hayat
 
Classes function overloading
ankush_kumar
 
CS225_Prelecture_Notes 2nd
Edward Chen
 
programing fundamentals complete solutions.docx
ZAMANYousufzai1
 
Lecture 8
Mohammed Saleh
 
Bca 2nd sem u-5 files & pointers
Rai University
 
Mca 2nd sem u-5 files & pointers
Rai University
 
C++ Course - Lesson 3
Mohamed Ahmed
 
Namespace--defining same identifiers again
Ajay Chimmani
 
Operators
moniammu
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Ameer Hamxa
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
1. DSA - Introduction.pptx
hara69
 
Bc0037
hayerpa
 
Namespace in C++ Programming Language
Himanshu Choudhary
 
Object Oriented Programming Using C++: C++ Namespaces.pptx
RashidFaridChishti
 
C++ FUNCTIONS-1.pptx
ShashiShash2
 
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
Ad

More from SAFFI Ud Din Ahmad (13)

PDF
Ammonia Industries
SAFFI Ud Din Ahmad
 
PDF
Phosphoric acid Industries
SAFFI Ud Din Ahmad
 
PDF
Oxygen and nitrogen Industries
SAFFI Ud Din Ahmad
 
PDF
Nitric acid Industries
SAFFI Ud Din Ahmad
 
PDF
Hydrogen Industries
SAFFI Ud Din Ahmad
 
PDF
Fertilizers Industries
SAFFI Ud Din Ahmad
 
PDF
Cuastic soda and Chlorine Industries
SAFFI Ud Din Ahmad
 
PPTX
Motion of particles in fluid (GIKI)
SAFFI Ud Din Ahmad
 
PPTX
Size reduction (GIKI)
SAFFI Ud Din Ahmad
 
PPTX
Principles of Combustion (GIKI)
SAFFI Ud Din Ahmad
 
PPTX
Liquid Fuels Lectures (GIKI)
SAFFI Ud Din Ahmad
 
PPTX
Particle Technology Lectures GIKI
SAFFI Ud Din Ahmad
 
PPTX
Fuels and Combustion Lectures (GIKI)
SAFFI Ud Din Ahmad
 
Ammonia Industries
SAFFI Ud Din Ahmad
 
Phosphoric acid Industries
SAFFI Ud Din Ahmad
 
Oxygen and nitrogen Industries
SAFFI Ud Din Ahmad
 
Nitric acid Industries
SAFFI Ud Din Ahmad
 
Hydrogen Industries
SAFFI Ud Din Ahmad
 
Fertilizers Industries
SAFFI Ud Din Ahmad
 
Cuastic soda and Chlorine Industries
SAFFI Ud Din Ahmad
 
Motion of particles in fluid (GIKI)
SAFFI Ud Din Ahmad
 
Size reduction (GIKI)
SAFFI Ud Din Ahmad
 
Principles of Combustion (GIKI)
SAFFI Ud Din Ahmad
 
Liquid Fuels Lectures (GIKI)
SAFFI Ud Din Ahmad
 
Particle Technology Lectures GIKI
SAFFI Ud Din Ahmad
 
Fuels and Combustion Lectures (GIKI)
SAFFI Ud Din Ahmad
 

18 dec pointers and scope resolution operator

  • 1. 1. C++ Scope Resolution Operator:: 2. Pointers C++ Scope Resolution Operator:: http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=% 2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm 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 return 0; }  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.
  • 2.  If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator. 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. #include <iostream> using namespace std; class X { public: static int count; }; int X::count = 10;// define static data member int main () { int X = 0; // hides class type X // use static member of class X cout << X::count << endl; }
  • 3. C++ Scope Resolution Operator :: PURPOSE 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. Namespaces Namespaces allow to group entities like classes, objects and functions under a name. In 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.
  • 4. 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.
  • 5. For example: // namespaces #include <iostream> using namespace std; namespace first { int var = 5; } namespace second { double var = 3.1416; } int main () { cout << first::var << endl; cout << second::var << endl; return 0; } EXAMPLE OUTPUT: 5 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.
  • 6. The 'using' Keyword The keyword using is used to introduce a name from a namespace into the current declarative region. For example: // using example #include <iostream> using namespace std; namespace first { int x = 5; int y = 10; } namespace second { double x = 3.1416; double y = 2.7183; } int main () { using first::x; using second::y cout << x << endl; cout << y << endl; cout << first::y << endl; cout << second::x << endl; return 0; } EXAMPLE OUTPUT: 5 2.7183 10 2.7183
  • 7. 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 #include <iostream> using namespace std; namespace first { int x = 5; int y = 10; } namespace second { double x = 3.1416; double y = 2.7183; } int main () { using namespace first; cout << x << endl; cout << y << endl; cout << second::y << endl; cout << second::x << endl; return 0; }
  • 8. EXAMPLE OUTPUT: 5 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:
  • 9. // using namespace example #include <iostream> using namespace std; namespace first { int x = 5; } namespace second { double x = 3.1416; } int main () { // Here are two blocks { using namespace first; cout << x << endl; } { using namespace second; cout << x << endl; } return 0; } EXAMPLE OUTPUT: 5 3.1416
  • 10. What Are Pointers? Reference: http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm  C++ pointers are easy and fun to learn.  Some C++ tasks are performed more easily with pointers, and  other C++ tasks, such as dynamic memory allocation, cannot be performed without them. As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory. Consider the following which will print the address of the variables defined:#include <iostream> using namespace std; int main () { int var1; char var2[10]; cout << "Address of var1 variable: "; cout << &var1 << endl; cout << "Address of var2 variable: "; cout << &var2 << endl; return 0; } When the above code is compiled and executed, it produces result something as follows: Address of var1 variable: 0xbfebd5c0 Address of var2 variable: 0xbfebd5b6
  • 11. What Are Pointers?  A pointer is a variable whose value is the address of another variable.  Like any variable or constant, you must declare a pointer before you can work with it.  The general form of a pointer variable declaration is: type *variablename; Here, type is the pointer's base type; it must be a valid C++ type and variablename is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid pointer declaration: int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // pointer to character The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
  • 12. There are few important operations Using Pointers in C++: Which we will do with the pointers very frequently: (a) We define pointer variables (b) Assign the address of a variable to a pointer and (c) Finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following example makes use of these operations: #include <iostream> using namespace std; int main () { int var = 20; // actual variable declaration. int *ip; // pointer variable ip = &var; // store address of var in pointer variable cout << "Value of var variable: "; cout << var << endl; // print the address stored in ip pointer variable cout << "Address stored in ip variable: "; cout << ip << endl; // access the value at the address available in pointer cout << "Value of *ip variable: "; cout << *ip << endl; return 0; }
  • 13. When the above code is compiled and executed, it produces result something as follows: Value of var variable: 20 Address stored in ip variable: 0xbfc601ac Value of *ip variable: 20 C++ Pointers in Detail: Pointers have many but easy concepts and they are very important to C++ programming. There are following few important pointer concepts which should be clear to a C++ programmer: Concept Description C++ Null Pointers C++ supports null pointer, which is a constant with a value of zero defined in several standard libraries. C++ pointer arithmetic There are four arithmetic operators that can be used on pointers: ++, --, +, - C++ pointers vs arrays There is a close relationship between pointers and arrays. Let us check how? C++ array of pointers You can define arrays to hold a number of pointers. C++ pointer to pointer C++ allows you to have pointer on a pointer and so on. Passing pointers to functions Passing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called function. Return pointer from functions C++ allows a function to return a pointer to local variable, static variable and dynamically allocated memory as well.
  • 14. C++ NULL Pointers  It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned.  This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.  The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. Consider the following program: #include <iostream> using namespace std; int main () { int *ptr = NULL; cout << "The value of ptr is " << ptr ; return 0; } When the above code is compiled and executed, it produces following result: The value of ptr is 0 To check for a null pointer you can use an if statement as follows: if(ptr) // succeeds if p is not null if(!ptr) // succeeds if p is null
  • 15. C++ Pointer Arithmetics  As you understood pointer is an address which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can a numeric value.  There are four arithmetic operators that can be used on pointers: ++, --, +, and - ptr++  the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer .  This operation will move the pointer to next memory location without impacting actual value at the memory location.  If ptr points to a character whose address is 1000, then above operation will point to the location 1001 because next character will be available at 1001.
  • 16. Incrementing a Pointer:  We prefer using a pointer in our program instead of an array because the variable pointer can be incremented, unlike the array name which cannot be incremented because it is a constant pointer.  The following program increments the variable pointer to access each succeeding element of the array: #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr; // let us have array address in pointer. ptr = var; for (int i = 0; i < MAX; i++) { cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the next location ptr++; } return 0; }
  • 17. Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in many cases. For example, a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing. Consider the following program: #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr; // let us have array address in pointer. ptr = var; for (int i = 0; i < MAX; i++) { cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the next location ptr++; } return 0; }
  • 18. When the above code is compiled and executed, it produces result something as follows: Address of var[0] = 0xbfa088b0 Value of var[0] = 10 Address of var[1] = 0xbfa088b4 Value of var[1] = 100 Address of var[2] = 0xbfa088b8 Value of var[2] = 200 MORE EXAMPLES #include <iostream> using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr[MAX]; for (int i = 0; i < MAX; i++) { ptr[i] = &var[i]; // assign the address of integer. } for (int i = 0; i < MAX; i++) { cout << "Value of var[" << i << "] = "; cout << *ptr[i] << endl; } return 0; } When the above code is compiled and executed, it produces following result: Value of var[0] = 10 Value of var[1] = 100 Value of var[2] = 200 You can also use an array of pointers to character to store a list of strings as follows: #include <iostream>
  • 19. using namespace std; const int MAX = 4; int main () { char *names[MAX] = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali", }; for (int i = 0; i < MAX; i++) { cout << "Value of names[" << i << "] = "; cout << names[i] << endl; } return 0; } When the above code is compiled and executed, it produces following result: Value of names[0] = Zara Ali Value of names[1] = Hina Ali Value of names[2] = Nuha Ali Value of names[3] = Sara Ali