Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT]

You're reading from   Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT] A practical, in-depth guide to implementing object-oriented design principles to create robust code

Arrow left icon
Product type Paperback
Published in Sep 2022
Publisher Packt
ISBN-13 9781804613900
Length 594 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Dorothy R. Kirk Dorothy R. Kirk
Author Profile Icon Dorothy R. Kirk
Dorothy R. Kirk
Arrow right icon
View More author details
Toc

Table of Contents (30) Chapters Close

Preface 1. Part 1: C++ Building Block Essentials
2. Chapter 1: Understanding Basic C++ Assumptions FREE CHAPTER 3. Chapter 2: Adding Language Necessities 4. Chapter 3: Indirect Addressing – Pointers 5. Chapter 4: Indirect Addressing – References 6. Part 2: Implementing Object-Oriented Concepts in C++
7. Chapter 5: Exploring Classes in Detail 8. Chapter 6: Implementing Hierarchies with Single Inheritance 9. Chapter 7: Utilizing Dynamic Binding through Polymorphism 10. Chapter 8: Mastering Abstract Classes 11. Chapter 9: Exploring Multiple Inheritance 12. Chapter 10: Implementing Association, Aggregation, and Composition 13. Part 3: Expanding Your C++ Programming Repertoire
14. Chapter 11: Handling Exceptions 15. Chapter 12: Friends and Operator Overloading 16. Chapter 13: Working with Templates 17. Chapter 14: Understanding STL Basics 18. Chapter 15: Testing Classes and Components 19. Part 4: Design Patterns and Idioms in C++
20. Chapter 16: Using the Observer Pattern 21. Chapter 17: Applying the Factory Pattern 22. Chapter 18: Applying the Adapter Pattern 23. Chapter 19: Using the Singleton Pattern 24. Chapter 20: Removing Implementation Details Using the pImpl Pattern 25. Part 5: Considerations for Safer Programming in C++
26. Chapter 21: Making C++ Safer 27. Assessments 28. Index 29. Other Books You May Enjoy

Recapping namespace basics

The namespace utility was added to C++ to add a scoping level beyond global scope to applications. This feature can be used to allow two or more libraries to be utilized without concern that they may contain duplicative data types, functions, or identifiers. The programmer needs to activate the desired namespace in each relevant portion of their application with the keyword using. Programmers can also create their own namespaces (usually for creating reusable library code) and activate each namespace as applicable. In the previous examples, we’ve seen the simple use of the std namespace to include cin and cout, which are instances of istream and ostream (whose definitions are found in <iostream>). Let’s review how we can create namespaces ourselves:

#include <iostream>
// using namespace std; // Do not open entire std namespace
using std::cout;   // Instead, activate individual elements
using std::endl;   // within the namespace as needed
namespace DataTypes
{
    int total;
    class LinkList
    {  // full class definition … 
    };
    class Stack
    {  // full class definition …
    };
};
namespace AbstractDataTypes
{
    class Stack
    {  // full class definition …
    };
    class Queue
    {  // full class description …
    };
};
// Add entries to the AbstractDataTypes namespace
namespace AbstractDataTypes   
{
    int total;
    class Tree
    {  // full class definition …
    };
};
int main()
{
    using namespace AbstractDataTypes; //activate namespace
    using DataTypes::LinkList;    // activate only LinkList 
    LinkList list1;     // LinkList is found in DataTypes
    Stack stack1;    // Stack is found in AbstractDataTypes
    total = 5;       // total from active AbstractDataTypes
    DataTypes::total = 85;// specify non-active mbr., total
    cout << "total " << total << "\n";
    cout << "DataTypes::total " << DataTypes::total;
    cout << endl;
    return 0;        
}

In the second line of the preceding code (which is commented out), we notice the keyword using applied to indicate that we’d like to use or activate the entire std namespace. Preferably, on the following two lines of code, we can instead activate only the elements in the standard namespace that we will be needing, such as std::cout or std::endl. We can utilize using to open existing libraries (or individual elements within those libraries) that may contain useful classes; the keyword using activates the namespace to which a given library may belong. Next in the code, a user specified namespace is created called DataTypes, using the namespace keyword. Within this namespace exists a variable total, and two class definitions: LinkList and Stack. Following this namespace, a second namespace, AbstractDataTypes, is created and includes two class definitions: Stack and Queue. Additionally, the namespace AbstractDataTypes is augmented by a second occurrence of the namespace definition in which a variable total and a class definition for Tree are added.

In the main() function, first, the AbstractDataTypes namespace is opened with the using keyword. This activates all names in this namespace. Next, the keyword using is combined with the scope resolution operator (::) to only activate the LinkList class definition from the DataTypes namespace. Had there also been a LinkList class within the AbstractDataType namespace, the initial visible LinkList would now be hidden by the activation of DataTypes::LinkList.

Next, a variable of type LinkList is declared, whose definition comes from the DataTypes namespace. A variable of type Stack is next declared; though both namespaces have a Stack class definition, there is no ambiguity since only one Stack has been activated. Next, we use cin to read into total, which is active from the AbstractDataTypes namespace. Lastly, we use the scope resolution operator to explicitly read into DataTypes::total, a variable that would otherwise be hidden. One caveat to note: should two or more namespaces contain the same identifier, the one last opened will preside, hiding all previous occurrences.

It is considered good practice to activate only the elements of a namespace we wish to utilize. From the aforementioned example, we can see potential ambiguity that can otherwise arise.

You have been reading a chapter from
Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT]
Published in: Sep 2022
Publisher: Packt
ISBN-13: 9781804613900
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image