Where can I place the constants my class needs to keep the global space free?

First: I know how to write a program, so I do not ask for help in this. However, I am inserting a copy of the problem so that you can see what the assignment implies. My question specifically addresses where you put the variables so as not to make everything global?

Appointment

Create a class named Date that has integer data items to hold the month, day, and year. The class must have a three-parameter default constructor, which allows you to set the date when creating a new Date object. If the user creates the Date object without passing any arguments or if any of the passed values ​​is invalid, the default values ​​of 1, 1, 2001 (i.e. January 1, 2001) should be used. The class must have member functions to print dates in the following formats:

3/15/10
March 15, 2010
15 March 2010

Questions

1) The teacher instructed us to avoid using magic numbers in our code, so the first question concerns my implementation of the default constructor:

// These are outside the class.
#define DEFAULT_MONTH 1
#define DEFAULT_DAY   1
#define DEFAULT_YEAR  2001

// This is inside the class definition.
Date(int month = DEFAULT_MONTH, int day = DEFAULT_DAY, int year = DEFAULT_YEAR);

Is it correct?

2) string, , , . enum ( switch).

const enum MONTH_IDS { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
    AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };

const string MONTH_NAMES[NUM_MONTHS] = { "January", "February", "March",
    "April", "May", "June", "July", "August", "September", "October",
    "November", "December" };

, ?

, ... , . , .

!

, , .

+5
3

1) . static const int - , , ... ?

struct Date {
    enum Constants {
        DEFAULT_YEAR = 2001,
        DEFAULT_MONTH = 1,
        DEFAULT_DAY = 1,
    };


    Date(int month = DEFAULT_MONTH, int day = DEFAULT_DAY, int year = DEFAULT_YEAR);

};

2) - , . ... , :

struct Date {
    std::string MonthToString(enum MONTH_IDS m) {
        static const char *monthNames[] = {
            "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December" };
        if(m >= sizeof(monthNames)/sizeof(monthNames[0]))
            return std::string("Unknown");
        return std::string(monthNames[m]);
    }
};
+4

, . , , :

// .h file
namespace mynamespace {
    extern const int foo;
};

// later, in a .cpp file
namespace mynamespace {
    const int foo = 42;
};

mynamespace::foo using namespace mynamespace; ( ) foo mynamespace. -, ( ) mynamespace, ( ).

enum - :

class foo {
  enum { CONST_FOO = 42, CONST_BAR = 24 };
};

; ( , const). , .

statics - :

void myclass::somefunction() {
    static const char *monthNames[] = { "JANUARY", ... };
    //...
}

, , , " ".

, enum ( ). , (, ) , , , .

+2

static const ( ), :

:

namespace ephaitch {
    extern const int Date_default_month;
    extern const int Date_default_day;
    extern const int Date_default_year;
    class Date {
        Date(int month = DEFAULT_MONTH, int day = DEFAULT_DAY, int year = DEFAULT_YEAR);
    };
}

:

namespace ephaitch {
    const int Date_default_month = 1;
    const int Date_default_day = 1;
    const int Date_default_year = 2001; 

    enum MONTH_IDS { JANUARY = 1, FEBRUARY, MARCH, APRIL, 
                     MAY, JUNE, JULY, AUGUST, 
                     SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER 
                   };

    const string MONTH_NAMES[NUM_MONTHS] = { 
         "January", "February", "March",
         "April", "May", "June", 
         "July", "August", "September", 
         "October", "November", "December" 
        };

    Date(int month, int day, int year)
    {
    }
}

DEFINE s, . enum , , .

0

All Articles