Consider a programming language with nested blocks where variables can be declared at different levels. If a variable x is declared in an outer block and another variable with the same name x is declared in an inner block, which variable is accessed inside the inner block?
Think about how inner blocks can override variables from outer blocks.
In nested blocks, the inner block's variable shadows the outer block's variable with the same name. So inside the inner block, the inner variable is accessed.
What is the primary purpose of the scope resolution operator :: in C++?
Consider how to refer to variables outside the current scope.
The scope resolution operator :: allows access to a global variable or a class member when there is a local variable with the same name shadowing it.
Given the following pseudo-code, what is the value printed?
int x = 5;
{
int x = 10;
{
print(x);
}
}Look at the closest declaration of x in the nested blocks.
The innermost block accesses the variable x declared in its immediate outer block, which has the value 10.
In a language supporting function overloading and scope resolution, what happens when you use the scope resolution operator to call a function from a base class that is overloaded in a derived class?
Think about how scope resolution can specify which class's function to invoke.
Using the scope resolution operator with the base class name explicitly calls the base class function, even if the derived class has an overloaded version.
In a multiple inheritance scenario where two base classes have a function with the same name, how does the scope resolution operator help resolve which function to call?
Consider how to specify the exact base class when functions have the same name.
The scope resolution operator allows the programmer to specify which base class's function to call, resolving ambiguity in multiple inheritance.
