5.11. Putting It All Together

Namespaces and Classes

Let's see how to use namespaces (page 132) with classes. The following header file defines a namespace for our Roman class (see Listing 4.9 on page 201).

Listing 5.13. Class Roman interface with namespaces
#ifndef ROMANH
#define ROMANH
// Roman.h - Roman class

namespace Anderson_Software_Group {
   const int Roman_max = 20;
   class Roman {
   private:
      int value;                    // decimal value
      char s[Roman_max];            // Roman String
      void convert_Roman();         // convert to Roman string
   public:
    // Constructors
      Roman(int n = 1) { value = n; convert_Roman(); }
    // Member Functions
      const char *getroman() const { return s; }
      int getnum() const { return value; }
   };
}
#endif

Namespace Anderson_Software_Group includes a constant ...

Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.