In this lesson, our instructor Alvin Sylvain goes through an introduction on namespaces. He discusses namespace for class association, how units can share namespace, using namespaces for types and classes, using explicit namespace, 'using' keyword, using for particular names, naming strategy, namespace aliases, and avoiding duplicates.
Copy a new version of your “Auto” homework. Remove every line that reads: using namespace std;
That will mean adding “std::” to every element in the standard namespace.
Create a new namespace for each object class you defined. Call it: homework_cplusplus_educator_com
In any block or CPP file where you may have numerous uses of “cout”, “endl”, etc., add: using std::cout;
or, “std::endl”, or which ever name you’d like to use without the “std::” qualifier.
If you put the “main” function inside the same namespace as the other classes, remove it from that namespace. (It should be in a separate file anyway.) Now you’ll need to reference your classes with their namespace, e.g., “homework_cplusplus_educator_com::Auto”.
Add a namespace alias, such as “homework”, so you won’t need to type the entire namespace name for “homework_cplusplus_educator_com.” (This is not just to save on your typing; the code will also be easier to read.)
Make sure everything still compiles and runs!
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.