C++ 作用域解析运算符
本节将讨论作用域解析运算符及其在C++编程语言中的各种用途。作用域解析运算符用于引用超出作用域的全局变量或成员函数。因此,我们使用作用域解析运算符来访问程序中隐藏的变量或函数。该运算符表示为双冒号(::)符号。
示例,在程序中,当全局变量或局部变量或函数具有相同的名称时,当我们调用变量时,默认情况下只访问内部或局部变量,而不是调用全局变量。通过这种方式,它隐藏了全局变量或函数。为了克服这种情况,我们使用作用域解析运算符来获取程序的隐藏变量或函数。
作用域解析运算符的用途
- 它用于访问程序的隐藏变量或成员函数。
- 它使用作用域解析定义类外部的成员函数。
- 它用于访问类的静态变量和静态函数。
- 作用域解析运算符用于继承中的函数覆盖。
使用作用域解析(::)运算符访问隐藏值的程序
Program1.cpp
#include
using namespace std;
// declare global variable
int num = 50;
int main ()
{
// declare local variable
int num = 100;
// print the value of the variables
cout << " The value of the local variable num: " << num;
// use scope resolution operator (::) to access the global variable
cout << "\n The value of the global variable num: " << ::num;
return 0;
}
输出
The value of the local variable num: 100
The value of the global variable num: 50
使用作用域解析运算符(::)在类外定义成员函数的程序
Program2.cpp
#include
using namespace std;
class Operate
{
public:
// declaration of the member function
void fun();
};
// define the member function outside the class.
void Operate::fun() /* return_type Class_Name::function_name */
{
cout << " It is the member function of the class. ";
}
int main ()
{
// create an object of the class Operate
Operate op;
op.fun();
return 0;
}
输出
It is the member function of the class.
使用作用域解析符(::)运算符演示标准命名空间的程序
Program3.cpp
#include
int main ()
{
int num = 0;
// use scope resolution operator with std namespace
std :: cout << " Enter the value of num: ";
std::cin >> num;
std:: cout << " The value of num is: " << num;
}
输出
Enter the value of num: 50
The value of num is: 50
使用作用域解析(::)运算符访问静态变量的程序
Program4.cpp
#include
using namespace std;
class Parent
{
static int n1;
public:
static int n2;
// The class member can be accessed using the scope resolution operator.
void fun1 ( int n1)
{
// n1 is accessed by the scope resolution operator (:: )
cout << " The value of the static integer n1: " << Parent::n1;
cout << " \n The value of the local variable n1: " << n1;
}
};
// define a static member explicitly using :: operator
int Parent::n1 = 5; // declare the value of the variable n1
int Parent::n2 = 10;
int main ()
{
Parent b;
int n1 = 15;
b.fun1 (n1);
cout << " \n The value of the Base::n2 = " << Parent::n2;
return 0;
}
输出
The value of the static integer n1: 5
The value of the local variable n1: 15
The value of the Base::n2 = 10
使用作用域解析运算符(::)访问静态成员函数的程序
Program5.cpp
#include
using namespace std;
class ABC
{
public:
// declare static member function
static int fun()
{
cout << " \n Use scope resolution operator to access the static member. ";
}
};
int main ()
{
// class_name :: function name
ABC :: fun ();
return 0;
}
输出
Use scope resolution operator to access the static member.
使用作用域限定符(::)重写成员函数的程序
Program5.cpp
#include
using namespace std;
class ABC
{
// declare access specifier
public:
void test ()
{
cout << " \n It is the test() function of the ABC class. ";
}
};
// derive the functionality or member function of the base class
class child : public ABC
{
public:
void test()
{
ABC::test();
cout << " \n It is the test() function of the child class. ";
}
};
int main ()
{
// create object of the derived class
child ch;
ch.test();
return 0;
}
输出
It is the test() function of the ABC class.
It is the test() function of the child class.