beginners constructor problem

I'm trying to do

a. create a constructor that takes 3 integers
i. in the order month, day, year
ii. assigns the month, day, year parameters to the corresponding data items.
iii. Use the ‘setter’ to assign the values
b. a 'default' constructor - no arguments
i. using the ‘setters’ assign
1. 1 to the month
2. 1 to the day
3. 1900 to the year

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
/****************************************
*     class definitions
****************************************/
class Date
{
    private:
       int  month;
       int  day;
       int  year;

    public:
       // setters
        void setMonth(int m) {month = m;}
        void setDay(int d) {day = d;}
        void setYear(int y) {year = y;}
        void setDate(int m,int d, int y) //should this
        {month = m; day = d; year = y;} //be here

        // getter
        int getMonth() {return month;}
        int getDay() {return day;}
        int getYear() {return year;}
        

       // 'member' functions
        bool calcLeapYear();
        void display();
        // constructors
        Date();
        Date(int month, int day, int year);


};
/****************************************
*     member functions for above class
****************************************/
bool Date :: calcLeapYear() 
{

    if(year % 400 == 0)
    {
        return true;
    }
    if(year % 100 == 0)
    {
        return false;
    }
    if(year % 4 == 0)
    {
        return true;
    }
    return false;
}

Date :: Date() //default constructor
{
    int month = 1;
    int day = 1;
    int year = 1900;

}

Date :: Date(int m, int d, int y) //constructor
{
    
    month = m;
    day = d;
    year = y;

}

void Date :: display()
{
    cout << endl;
    cout << "The month is " << getMonth() << endl;
    cout << "The day is " << getDay() << endl;
    cout << "The year is " << getYear() << endl;

    bool calcLeapYear(getYear());
    if (calcLeapYear == true)
    {
        cout << getYear() << "is a leap year" << endl;
    }
    else
    {
        cout << getYear() << "is not a leap year" << endl;
    }
}

   void testDate02() //tests constructors
{
     cout << "*** test 02 ***\n";

     Date d2;
    d2.setDate(12,25,2000); //how am I screwing this up?



}
closed account (48T7M4Gy)
By the look of it you have no ...

1
2
3
4
5
6
7
8
int main()
{ 
     Date d2;
    d2.setDate(12,25,2000); //how am I screwing this up?

    // add sum display stuff
    // return 0;
}


testDate02 is just sitting out there all by its lonesome otherwise, just waiting for sumpin' to happen - or worse :)
Last edited on
closed account (48T7M4Gy)
Even better why not try out the constructor:

1
2
3
4
5
6
7
int main()
{ 
     Date d2(12, 25, 2000);

    // add sum display stuff
    // return 0;
}

thanks a lot man

while were on it, why is the display

1
2
3
4
5
6
7
8
9
bool calcLeapYear(getYear());
    if (calcLeapYear == true)
    {
        cout << getYear() << "is a leap year" << endl;
    }
    else
    {
        cout << getYear() << "is NOT a leap year" << endl;
    }


giving me "is a leap year" every time?
Last edited on
closed account (48T7M4Gy)
I don't have enough to run your program but line 79 above is ringing alarm bells to me. Shouldn't this be a separate method, also without a ; at the end?

bool calcLeapYear( ) as a class method using private year property would be better and probably work with the logic you have.

In main your program line would then be
bool leapTest = calcLeapYear( d2.getYear() );
Last edited on
Topic archived. No new replies allowed.