Broad Network


Use of C++ Scope Resolution Operator

Some Features of C++ Entities – Part 4

Forward: In this part of the series, I explain the use of C++ scope resolution operator.

By: Chrysanthus Date Published: 26 Aug 2012

Introduction

This is part 4 of my series, Some Features of C++ Entities. In this part of the series, I explain the use of C++ scope resolution operator. The scope resolution operator is the double colon operator. It is used with class member functions, class static data members and the namespace.

Used with the Class
The scope resolution operator is used to define function members (methods) outside of the class description. The following program illustrates this:

#include <iostream>
using namespace std;

class Calculator
    {
        public:
        int num1;
        int num2;

        Calculator()
            {
                num1 = 11;
                num2 = 12;
            }

        int add ();
    };


int Calculator::add ()
            {
                int sum = num1 + num2;
                return sum;
            }


int main()
    {
        Calculator myObject;
        int result = myObject.add();
        cout << result;

        return 0;
    }

The line that uses the scope resolution operator in the above code is the interface:

    int Calculator::add ()

Note how :: has been used in the line. This method of use also applies to the constructor.

Use with Class Static Member
The scope resolution operator can be used to access the static data member of a class. The following program illustrates this:

#include <iostream>
using namespace std;

class MyClass
    {
        public:
        static int sameAll;
    };

int MyClass::sameAll = 5;

int main()
    {

        MyClass myObj;
        myObj.sameAll = 6;
        cout << MyClass::sameAll;
        
        return 0;
    }

The line that uses the scope resolution operator in the above code is the statement:

    int MyClass::sameAll = 5;

Note how :: has been used in the line.

Used with Namespace
The scope resolution operator can be used to access the namespace identifiers outside the namespace description (definition). The following program illustrates this:

#include <iostream>
using namespace std;

namespace myName
    {
        int ident1 = 33;
        int ident2 = 40;
        int herInt = ident1;
    }

int hisInt = myName::ident1;

int main()
    {
        cout << hisInt << '\n';
        cout << myName::ident2 << '\n';
        cout << myName::herInt;

        return 0;
    }

The lines in which the scope resolution operator has been used in the above programs are:

    int hisInt = myName::ident1;
    cout << myName::ident2 << '\n';
    cout << myName::herInt;

Note how :: has been used in the lines.

There may be other uses of the scope resolution operator. However, I have given you the main uses.

That is it for this part of the series. We stop here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message