The basics of programming in c++ for beginners

Area of visibility of variables (part 2)

You were introduced to the first article on the topic Local and global variables. Area of visibility in C ++. I would like a little more to complement it. Namely example with namespaces. I wrote a prefix is ​​not in vain std:: before the output statement cout. and endl. It is also one of the formations area of visibility – namespace. If you do not go into specific details, namespace, is a certain group of operators / function / variables / types, united in this Statement “fraction” With name std.

std namespace of the header file (header in our) iostream. cout, cin, endl and etc. – it's all “dynasty” std. If you do not plug it into the code and write std:: before these operators, C ++ will tell “bye Bye”. Because he does not know, what is this cout so without “surname”. By the way in C ++ and coined the operator using namespace, which allows you to write the name of the space as a prefix to each of its member operators . This is done for convenience, but you need to be careful that the operator, if there is a function of the same name used in the program from different namespaces.

The namespace is good there, where it is necessary to group something in the area of visibility by name. For example in the program two different functions can be described by sum(). One returns an integer, second real. If the task can not be described as overloaded, it can be screwed into their namespace:

The namespace defines a scope here for various functions with the same name. Outside the namespace of these functions does not exist.

c++, local and global variables, the scope of variables, c ++ beginners, namespace

In general, a namespace can become a point of contention concerning the area of visibility of the theory. In fact it's just a group named, but it has the same effect, as well as a block of statements {…}. Do you want to apply to the operators of this space – be kind to turn first to the name space: integer :: sum()    Tut colon ::  – an operator extension of area of visibility.

Perhaps the last, we can say – the area of visibility is very revealing in OOP (Object Oriented Programming). When in class programmer describes private property (variable or function method), he asks her area of visibility. For example, in the type code:

properties k and kc They have areas of visibility. The first is available only within the class methods. For it can not be accessed in the main program, it is private (private ). Closed to all, except in the area of visibility of the class.

The second public (public). Its area of visibility extends beyond the class. From the program, which will be called an object of this class is available.

c++, local and global variables, the scope of variables, c ++ beginners, namespace

kc It is declared as a public variable, its area of visibility, unlike the change k , wider. Therefore, the compiler does not against its use outside of class.

What can we draw from all this conclusion? Geeks foaming at the mouth screaming on all portals – “Down with global variables!”. Geeks like to limit the area of visibility, locating and packaging everything into classes or namespaces. Geeks there are geeks. The programmer must not abuse the area of visibility, not to get, rendering, when to refer to a variable you need to write it the way. That is. something like myname::group1::PeremenkiDlaPrepoda::i Well, just a tin.

The use of global variables, along with the local should be wisely – where appropriate write global. Where there is no – localize.

If you have not watched the video on the subject area of visibility in C ++ in the previous lesson – and place it here:

One thought on “Area of visibility of variables (part 2)

  1. You have earned 44 842 US dollars. GET >>> https://forms.yandex.com/cloud/65c5cc5a2530c245feb4aa15/?hs=06d74f4977fe77ae66d58de35bd62db6& says:

    r5av7s

Leave a Reply

Your email address will not be published. Required fields are marked *