The following video will teach you about the scope in C++, scope resolution operator and about namespaces. This video has the easiest explanation.
using namespace std; means that you are going to use namespace which is std.you can remove this line and run this code with the help of scope resolution operator, to learn about it please watch the video and like it. Click HERE to watch the video.
What is scope?
In C++, we usually define the scope with curly braces. You can think of scope as a boundary for a thing and that thing cannot access anything outside that boundary but inside that boundary, it can access the things which are inside that boundary. To use this approach we use curly braces {}.
Suppose I have two scopes that contain two functions.
SCOPE A
{
cout
}
SCOPE B
{COUT can not access instructions that are in SCOPE B because instructions are outside the boundary of Scope A.
Instructions
}
What are namespaces?
As you read above about scope if we name each scope that scope will become namespace. In other words, if you name a boundary that boundary and the instruction within that boundary or scope become namespace let me show you:
namespace std
{
cout
}
namespace somethingwe use namespaces to differentiate between instructions as well as to access instructions without rewriting the whole instructions. we can use a namespace with this statement.
{
Instructions
}
using namespace std;
By this statement whenever we will write COUT in our main function compiler will know because of this statement that to execute this COUT we have instructions in STD namespace and the compiler will use that instruction to print the character on screen.
Scope resolution operator:
What if we want to execute COUT without writing using namespace std; then we will use scope resolution operator, this operation has a symbol which is consist of two colons "::" this operator will tell the compiler that, go to std namespace there will you find the instructions to execute this statement. Like this
#include <iostream>
int main()
{
std::cout<<"Hello Word! Subscribe to mastertraders.";
}
That's all. Watch the above video for better understanding.
No comments:
Post a Comment