help overloading constructor

i'm having problems here, i don't know what exactly i'm doing wrong.
i'm supposed to overload the constructor and call them both but each time i run the program everything is set to the first constructor.

1
2
3
4
5
6
7
8
Date() : day(20), month(9), year(2090)
    { 
        cout << "\nconstructor current date";
    }
    Date(int d, int m, int y): day(d), month(m), year(y) 
    {
        cout << "\nconstructor set date";
     }


what am i doing wrong ?
thanks in advance

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <windows.h>

using namespace std;
char span=164;

class Date
{
    private:
        int day;
        int month;
        int year;
    public:
    Date() : day(20), month(9), year(2090)
    { 
        cout << "\nconstructor current date";
    }
    Date(int d, int m, int y): day(d), month(m), year(y) 
    {
        cout << "\nconstructor set date";
     }
    void setDate()
    {
        cout << "\n\nDigite la fecha en el formato dia, mes, a" << span << "o Ej: 19 06 2012.\n";
        cin >> day >> month >> year ;
    }
    void setDate(int day, int month, int year)
    {
        cout<< "\nsetdate 2";
    }
    void showDate()
    {
        cout<< "\nday "<< day << " month " << month << " year " << year;
    }
    //void showDate()
    //{

    //}
    //void showdateWords()
    //{

    //}
    void showDateWords()
    {
        static const int MAX = 12;
        char months[MAX][MAX]={"Enero","Febrero","Marzo","Abril","Mayo",
                            "Junio","Julio","Agosto","septiembre",
                            "Octubre","Noviembre","Diciembre"};
        cout << "\nLa fecha es "<< day << " de " << months[month] << " del a"<< span << "o " << year;
    }
    bool validate();
};
bool Date::validate()
{
    int leapYear;

    leapYear=(year%4==0||(year%100==0 && year%400==0));
    bool verify ((day<1 || day >31 || month<1 || month>12 ) || ((month==4 || month==6
                     || month==9 || month==11 )&& day > 30 )  || ((leapYear==true && month==2 && day>29) 
                      || (leapYear!=true && month==2 && day>28)));

    if (verify == true )
    {

      cout << "\n\n\terror: fecha incorrecta. " ;
    }

    return ( verify?true:false);

}


int main()
{
    SYSTEMTIME t;
        GetLocalTime(&t);
        int cDay= t.wDay;
        int cMonth= t.wMonth;
        int cDayofweek= t.wDayOfWeek;
        int cYear= t.wYear;

    Date d1, d2, d3;

    d1.setDate(cDay, cMonth, cYear);
    d3.setDate(1, 1, 2000);
    //do
    //{
    //    d2.setDate();
    //}while(d2.validate()==true);


    d1.showDate();
    d2.showDate();
    d3.showDate();
    d1.showDateWords();
    d2.showDateWords();
    d3.showDateWords();

}
Last edited on
Date d1, d2, d3;

you are calling the default constructor on each of these, which is this one:

Date() : day(20), month(9), year(2090)
{
cout << "\nconstructor current date";
}

try this:

Date d1;
Date d3(1, 1, 2000);

You need to actually pass arguments to the constructor when you declare the object:
1
2
Date d1; //This calls the default constructor which takes no args.
Date d2(1, 12, 1998); //This calls the overloaded constructor. 
1
2
3
4
   Date() : day(20), month(9), year(2090)
    { 
        cout << "\nconstructor current date";
    }


It looks like "back in the future!":)
Last edited on
thanks guys that was really helpful.
Last edited on
now i'm having another problem , i'm doing something but not sure if it's possible or should be done

1
2
3
4
5
6
7
8
void showDateWords()
    {
        Date.words(Date);               // i'm not sure if allowed to do this
    }                                             
    void showDateWords(char a)  // other possibility for overloading ?
    {
        Date.words(Date);
    }


i get error
error: expected unqualified-id before '.' token|

also as part of my homework i'm supposed to overload every function but i don't like
how these looks just adding the (char a) i'm not actually passing any new information
to the function i just need to overload it and have it show the same info.
how should i do it ?

thanks for the help

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <windows.h>

using namespace std;
char span=164;

class Date
{
    private:
        int day;
        int month;
        int year;
    public:
    Date() : day(20), month(9), year(2090)
    {
        cout << "\nconstructor current date";
    }
    Date(int d, int m, int y): day(d), month(m), year(y)
    {
        cout << "\nconstructor set date";
     }

    void setDate()
    {
        cout << "\n\nDigite la fecha en el formato dia, mes, a" << span << "o Ej: 19 06 2012.\n";
        cin >> day >> month >> year ;
    }
    void setDate(int day, int month, int year)
    {
        cout<< "\nsetdate 2";
    }
    void showDate()
    {
        cout<< "\nday "<< day << " month " << month << " year " << year;
    }
    void showDate(char a)
    {
        cout<< "\n" << day << '/' << month << '/' << year ;
    }
    void showDateWords()
    {
        Date.words(Date);
    }
    void showDateWords(char a)
    {
        Date.words(Date);
    }
    bool validate();
    void words();
};
bool Date::validate()
{
    int leapYear;

    leapYear=(year%4==0||(year%100==0 && year%400==0));
    bool verify ((day<1 || day >31 || month<1 || month>12 ) || ((month==4 || month==6
                     || month==9 || month==11 )&& day > 30 )  || ((leapYear==true && month==2 && day>29) 
                     || (leapYear!=true && month==2 && day>28)));

    if (verify == true )
    {

      cout << "\n\n\terror: fecha incorrecta. " ;
    }

    return ( verify?true:false);

}
void Date::words()
{
    static const int MAX = 12;
        char months[MAX][MAX]={"Enero","Febrero","Marzo","Abril","Mayo",
                            "Junio","Julio","Agosto","septiembre",
                            "Octubre","Noviembre","Diciembre"};
        cout << "\nLa fecha es "<< day << " de " << months[month-1] << " del a"<< span << "o " << year;
}

int main()
{
    SYSTEMTIME t;
        GetLocalTime(&t);
        int cDay= t.wDay;
        int cMonth= t.wMonth;
        int cYear= t.wYear;

    Date d1, d2;
    Date d3(1, 1, 2000);
    Date d4(cDay, cMonth, cYear);


    do
    {
       d2.setDate();
    }while(d2.validate()==true);


    d1.showDate();
    d2.showDate();
    d3.showDate();
    d4.showDate();
    d1.showDateWords();
    d2.showDateWords();
    d3.showDateWords();
    d4.showDateWords();

    cout<< "\n\nmetodo para modificar fecha: ";
    d3.setDate();
    d2.setDate(31,06, 2098);
    d1.setDate();


    d1.showDate('a');
    d2.showDate('a');
    d3.showDate('a');
    d4.showDate('a');
    d1.showDateWords('a');
    d2.showDateWords('a');
    d3.showDateWords('a');
    d4.showDateWords('a');

    return 0;
}
There is no such member function declaration as void words(). So the function definition

void Date::words()
...

is invalid.

Oh, I am sorry. You duplicated your message.

The problem is that you call the function incorrectly
In this expression

Date.words(Date);

instead of the class name Date you shall specify an object of type Date.

Last edited on
i don't know how should i call it, could you please give me an example , thanks

Last edited on
Declare the overloaded functions showDateWords

1
2
3
4
5
6
7
8
    void showDateWords()
    {
        words();
    }
    void showDateWords(char a)
    {
        words();
    }


By the way it is not clear what is the purpose of the presence of the parameter in the second function?
thanks vlad for the help, the teacher just want us to overload the constructor and the member functions.
the functions don't need to show new information nor take new information, so i thought just adding (char a) was enough.

but if there's a better way to do it, i would like to learn.

thanks again.



Last edited on
Topic archived. No new replies allowed.