Help with Date Class assignment

So I'm fairly new to coding with classes and even though I understand the basic concept behind classes and how to work with simple examples, I'm still struggling with certain declarations of member functions.
Here is my code:

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
#include <iostream>
#include <string>

using namespace std;

class Date{
    private:
        unsigned day;
        unsigned month;
        string monthName;
        unsigned year;
    public:
        Date();
        Date(unsigned m, unsigned d, unsigned y);
        Date(const string &mn, unsigned d, unsigned y);
        void printNumeric() const;
        void printAlpha() const;
    private:
        bool isLeap(unsigned) const;
        unsigned daysPerMonth(unsigned m, unsigned y) const;
        string name(unsigned m) const;
        unsigned number(const string &mn) const;
};

Date::Date (){
    day = 1;
    month = 1;
    monthName = "January";
    year = 2000;
}

Date::Date(unsigned m, unsigned d, unsigned y){
    month = m;
    day = d;
    year = y;
}

Date::Date(const string &mn, unsigned d, unsigned y){
    monthName = mn;
    day = d;
    year = y;
}

bool Date::isLeap(unsigned year) const{
    int x;
    if ( (year % 4) == 0){
        x = 5;
        if ( (year % 100) == 0){
            x = 0;
            if ( (year % 400) == 0)
                x = 5;
        }
    }
    if ( x == 5 )
        return true;
    else    
        return false;
}

void Date::printNumeric() const{
    cout << month << "/" << day << "/" << year;    
}

void Date::printAlpha() const{
    cout << monthName << " " << day << ", " << year;   
}


int main(){
    Date today, tomorrow(11,16,1994), yesterday("November",16,1994);
    today.printNumeric();
    cout << "\n";
    today.printAlpha();
    cout << "\n";
    tomorrow.printNumeric();
    cout << "\n";
    yesterday.printAlpha();
    cout << "\n";
    //This is the piece I'm having trouble with
    //How do I make use of my bool class function to check for leap years?
    yesterday.isLeap(1994);
    cout << "\n";  
    //If what I'm attempting to do looks stupid it's because I have no idea how
    //to make this work
}


I've been using my main to test sections of code and all my public member functions work fine, but I can't seem to get any of my private member functions to work.

As you can probably tell from my class declaration I still have more private member functions I need to define, but hopefully if this first one is explained I can figure out the rest.

EDIT: The error the the compiler returns is the following (using g++):
date_class.cpp:44: error: 'bool Date::isLeap(unsigned int) const' is private
date_class.cpp:146: error: within this context
Last edited on

Declaring isLeap under private is just that, private and therefore means anything outside of the class object cant see it or use it. Same with the other functions.

Those which are private can only be used by the class object itself.




One last thing I noticed, the isLeap returns a boolean but you are not checking against its return value. For example:

1
2
3
if (yesterday.isLeap(1994)) {
	// I'm a leap year
}

So what parameter should be used when I attempt to use the isLeap function? The object being used should already have a year defined to it so what do I put in the parentheses?
One last thing I noticed, the isLeap returns a boolean but you are not checking against its return value. For example:

1
2
3
if (yesterday.isLeap(1994)) {
	// I'm a leap year
}



Like I said in the OP I was just testing values in main to make sure that sections of my code are working properly. I was expecting a 1 to be returned for true if it was a leap year, and a 0 to be returned if it was not a leap year. I haven't implemented this information yet. Unless I misunderstood your post.
Last edited on

Basically as I said in my previous post, for any function outside of the class (i.e. your main) to use class member functions they cant be declared as private.

Correct me if I am wrong, I would assume that the IsLeap() would be checking the year in unsigned year; of your class - if this is the case you could just make the function public, and there would be no need to pass a year as its already in the class.

So in your code you initialised tomorrow(11,16,1994) so then you would just run the isLeap() to determine if that year was a leap year? - or am i on the wrong track of what you are trying to achieve?




If you wanted to use a setter function rather than assigning the variables from the constructor (constructors cant return values) you could do it 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
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

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

class CDate
{
	private:
		unsigned day;
		unsigned month;
		unsigned year;
		string monthName;

	public:
		CDate() { day = 0; month = 0; year = 0; monthName = ""; }
		~CDate() {}

		// these two setter functions return true if the given
		// year is a leap year, otherwise false.
		bool setDate(unsigned m, unsigned d, unsigned y);
		bool setDate(const string &mn, unsigned d, unsigned y);
		// isLeap provided public if we want to use it that way
		bool isLeap();
		// display date function checks length of alpha month
		// to see if its over 0 length, if true then it assumes
		// we want to show the alpha version of the date.
		void dispDate();
};


bool CDate::setDate(unsigned m, unsigned d, unsigned y)
{
	month = m;
	day = d;
	year = y;
	return(isLeap());
}

bool CDate::setDate(const string &mn, unsigned d, unsigned y)
{
	monthName = mn;
	day = d;
	year = y;
	return(isLeap());
}

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

void CDate::dispDate()
{
	// if monthName is above 0 length then
	// assume we are a alpha month
	if (monthName.length() > 0)		// are we word version
		cout << endl << monthName << " " << day << ", " << year;
	else
		cout << endl << month << "/" << day << "/" << year;
}


int main()
{

	CDate Dates[2];

	// we could do it like this..
	if (Dates[0].setDate(11, 16, 2012))
		cout << endl<< "Im a leap year!!";
	else
		cout << endl << "Im not a leap year!!";

	// or like this
	Dates[1].setDate("November", 16, 2014);
	if (Dates[1].isLeap())
		cout << endl << "Im a leap year!!";
	else
		cout << endl << "Im not a leap year!!";

	// display them
	Dates[0].dispDate();
	Dates[1].dispDate();

	return 0;
}


Topic archived. No new replies allowed.