Scope Resolution Operator, Namespaces, Enum Classes

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=WadFfuYmG0E



Duration: 1:15:28
110 views
0


Today we made a simple inventory system in C++ using classes and a vector.


There's a couple major learning points for today.


1) The Scope Resolution Operator (::) allows you to select between different scopes (a scope is a pair of matching curly braces) to say which variable or whatever you are talking about. After all, you can have a local variable named x, a global variable named x (called ::x), an x in the std namespace (std::x), an x in the Kerney namespace (Kerney::x) an x in an enum class (ITEM_TYPE::x) an x in a class (classname::x) and so forth.


The :: basically means "inside of" and is used to specify which x you're talking about.


2) Static variables inside of a class are associated with the class, not just with objects of that class. So if you had an Insect class that had a const static int MAX_LEGS = 6, you could cout Insect::MAX_LEGS to print the number 6 before you even make any variables of Insect type.


3) You can use enums to make a list of possible values for a variable. If you have a small finite list (for example, menu options in a GUI) they work really well, and prevent accidental mistakes from using an int or something to represent them.


You make an enum class like this:

enum class OPTIONS {A,B,C};


Then make a variable of this type like this:
OPTIONS bob = OPTIONS::C;


4) Namespaces are a way of hiding your names from the global namespace so that they can be used with other people's code without causing a name conflict.







Tags:
csci 40
c++
namespace
scope resolution operator
enums
enum class