Multi-Paradigm Programming in C++: Namespaces and Nested Classes

e81 cse 428s n.w
1 / 4
Embed
Share

Learn about the significance of namespaces and nested classes in C++ programming, how they help avoid naming collisions, and how to declare and define them effectively. Studio 7 is included for practical exercises. Submit your work before October 16th for feedback and potential resubmission.

  • C++
  • Namespaces
  • Nested Classes
  • Programming
  • Studio

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. E81 CSE 428S: Multi-Paradigm Programming in C++ Namespaces and Nested Classes Department of Computer Science & Engineering Washington University, St. Louis MO Chris Gill cdgill@wustl.edu 1

  2. Namespaces Namespaces partition the global namespace [LLM] Avoids collisions between symbols with the same name May risk collisions if you say using namespace in .h files That s also only ok-ish in .cppfiles where it s better to say using std::ostream; and using std::vector; A namespace is a scope [LLM] Header files should use the scoping operator for standard (or other namespace) types, e.g., std::vector etc. The global namespace is indicated by the :: operator with no other symbol in front of it, e.g., ::std::ostream A program s main function is in the global namespace When to introduce a namespace If other scopes (classes etc.) don t distinguish symbols If you want to collect related things in a common scope (which can be done recursively by nesting namespaces) Conveniently, can declare a namespace discontinuously CSE 428S Multi-Paradigm Programming in C++ 2

  3. Nested Classes Nested classes are most often used to define implementation classes [LLM] E.g., the LLM TextQuery::QueryResult nested class I would argue (with LLM) that nested classes are related to the enclosing class, but may somewhat weakly so The nested class is scoped within the enclosing class So, putting their declarations and definitions into the same header and source files (respectively) may be reasonable Declaring a nested class (in a header file) The enclosing class can simply forward declare the nested one, e.g., public: class QueryResult; The nested class declaration then is given like any other class except that its class name is scoped to the enclosing class, as in class TextQuery::QueryResult { } Defining a nested class (in a source file) Nested class definitions are also similarly scoped, as in TextQuery::QueryResult::QueryResult ( ) { } CSE 428S Multi-Paradigm Programming in C++ 3

  4. Studio 7 Try different ways of accessing types that already are scoped within namespaces Open and close namespaces of your own Declare and define types within those namespaces Studios 0 through 11 are due 11:59pm Monday October 16th (the night before Exam 0) Submit as soon as each is done so you get feedback and can resubmit any that may be marked incomplete CSE 428S Multi-Paradigm Programming in C++ 4

More Related Content