18 dec pointers and scope resolution operator

19
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.

Upload: saffi-ud-din-ahmad

Post on 02-Jul-2015

122 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 18 dec pointers and scope resolution operator

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.

Page 2: 18 dec pointers and scope resolution operator

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;

}

Page 3: 18 dec pointers and scope resolution operator

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.

Page 4: 18 dec pointers and scope resolution operator

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.

Page 5: 18 dec pointers and scope resolution operator

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.

Page 6: 18 dec pointers and scope resolution operator

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

Page 7: 18 dec pointers and scope resolution operator

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;

}

Page 8: 18 dec pointers and scope resolution operator

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:

Page 9: 18 dec pointers and scope resolution operator

// 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

Page 10: 18 dec pointers and scope resolution operator

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

Page 11: 18 dec pointers and scope resolution operator

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.

Page 12: 18 dec pointers and scope resolution operator

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;

}

Page 13: 18 dec pointers and scope resolution operator

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.

Page 14: 18 dec pointers and scope resolution operator

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

Page 15: 18 dec pointers and scope resolution operator

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.

Page 16: 18 dec pointers and scope resolution operator

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;

}

Page 17: 18 dec pointers and scope resolution operator

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;

}

Page 18: 18 dec pointers and scope resolution operator

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>

Page 19: 18 dec pointers and scope resolution operator

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