Default Constructor

...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
class Date {
public:

    // Date(); // *** commented out (also remove its definition in the .cpp file).
    
    // this constructor can be called with no arguments 
    // (there is a default argument for every parameter) 
    // so it is used as the default constructor 
    Date(int m = 1, int d = 1, int y = 2000);

    // ...
};
...
Last edited on
I would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class date
{
    public:

        enum format_t { DEFAULT, LONG, TWO_DIGIT } ;

        // ....

        format_t format() const { return current_format ; }
        format_t format( format_t f ) { return current_format = f ; }

        void print() const
        {
            switch(current_format)
            {
                case LONG : return print_long() ;
                case TWO_DIGIT : return print_two_digit() ;
                case DEFAULT : return print_default() ;
            }
        }

    private:

        format_t current_format = DEFAULT ;

        void print_default() const { /* print default format */ }
        void print_long() const { /* print long format */ }
        void print_two_digit() const { /* print two-digit format */ }
};


And then,
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    date some_date { /* ... */ } ;
    // ...
    some_date.print() ;

    some_date.format( date::LONG ) ;
    some_date.print() ;

    // etc.
}
Topic archived. No new replies allowed.