0
CPP OOP
What is scope in programming? (using namespace std;) what is "Public:" function in CPP OOP ?
2 Respuestas
+ 3
Scope refers to the range of accessibility of whatever it is you're talking about (variable, class, function).
using namespace std makes everything from the standard namespace accessible anywhere in the file you are working with.
namespace std spans multiple files, like iostream, iomanip, vector, etc. just to name a few. In each of these files, there is namespace std, where practically everything in the standard file is declared.
For example, if I put #include <iostream> using namespace std; at the beginning of my cpp program file, then everything from the std namespace in the iostream file is accessible to me. This includes cout, cin, endl, etc.
If you don't include the line using namespace std; you have to specify the scope of the thing you want to use, by specifying the namespace the thing is from (std in this example), followed by the scope resolution operator which is written as two colons, ::, and followed by the thing you want to use, e.g., std::cout, instead of cout when we had using namespace std;
public: is an access specifier of a class, which is a user defined data-type. public means that anything can access it. For example, if we have the class
class Dog{
public: int legs;};
and we declared a Dog object.
Dog sam;
we can access the legs attribute from outside the class.
sam.legs = 4; cout << sam.legs;
However, if we changed public to private, then this would give an error because the attribute legs is not accessible outside the class.
+ 1
Awesome, thank you Edward!