2

I'm reading through A Complete Guide to Programming in C++ written by Ulla Kirch-Prinz and Peter Prinz, published by Jones & Bartlett. In it, there is an example of a class (page 246) with what I think is a major problem--not in that it is an error but something I've heard you are never supposed to do:

// account.h
// Defining the class Account.
// ---------------------------------------------------
#ifndef _ACCOUNT_       // Avoid multiple inclusions.
#define _ACCOUNT_

#include <iostream>
#include <string>
using namespace std;

class Account
{
    private:                // Sheltered members:
        string name;        // Account holder
        unsigned long nr;   // Account number
        double balance;     // Account balance

    public:                 //Public interface:
        bool init( const string&, unsigned long, double);
        void display();
};

#endif   //  _ACCOUNT_

The include guard's macro begins and ends with underscores. From what I've been told, underscores are reserved for use by the Standard Library, the operating system, and the compiler and unless you are creating an operating system or compiler, there's pretty much no reason to ever use it.

I get that this is just an example but for the intents and purposes of teaching people good programming habits, it still seems like they messed up badly with this one. Having said that, I'm still pretty new to this so I could be wrong and it is appropriate.

thepufferfish
  • 441
  • 4
  • 17

1 Answers1

4

Yes, these are reserved names, though you are unlikely to actually run into a problem.

Note that in this case it's only reserved because it is followed by an uppercase letter. +account would be fine.

And yes, I would agree that books really should not be perpetrating this style. It basically stems from people having gotten used to the style by looking at implementation library headers (which are allowed to use reserved names) and thinking "so that's how it's done" without realizing that those names are used specifically because they are reserved.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • Yeah, I get that they are unlikely to cause an issue but what I mean to say is, is this bad programming practice? – thepufferfish Jul 12 '20 at 19:32
  • Yes it is, as is [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Paul Sanders Jul 12 '20 at 19:50
  • @PaulSanders at this beginner level using namespace std is fine, it is too much to ask a beginner like this to define their our namespaces.... – Yunfei Chen Jul 12 '20 at 23:00
  • @YunfeiChen That's not what I'm suggesting. Instead of `using namespace std` one should explicitly prefix symbols in the `std` namespace with `std::`, for reasons that the link I provided explains. If you don't do that, it will turn round and bite you sooner or later. – Paul Sanders Jul 12 '20 at 23:02
  • Yep that which point you would already be defining your own namespaces.... – Yunfei Chen Jul 12 '20 at 23:08