I am trying to implement a function sameMonth from a class to compare two user entered months.

My class is declared in a header file "Date.h":

#include <iostream>
using namespace std;


class Date
{
    private:
        string month;
        int day;
        int year;
    public:
        Date();
        Date(string aMonth, int aDay, int aYear);
        void setMonth(string month);
        void setDay(int aDay);
        void setYear(int aYear);
        string getMonth();
        int getDay();
        int getYear();
        void printDate();
        bool sameMonth(string aMonth, string aMonth2);
};

The function sameMonth itself exists in another file "Date.cpp"

bool sameMonth(string aMonth, string aMonth2)
{
    if (aMonth == aMonth2)
    {
        cout << "The two months entered are the same" << endl;
        return true;
    }
    return false;
    cout << "The two months are different" << endl;
}

The rest of the program is run from a "main.cpp" file. Here I am tying to compare the two months stored in two of my class objects. However, I cant seem to figure out exactly how to call the function.

Here is my attempt (code snippet).

    bool testMonth;  
    Date compare; 

    testMonth = compare.sameMonth(aMonth, aMonth1);

Any suggestions?

Date.h requires an include guard.
http://en.wikipedia.org/wiki/Include_guard

Either:

////////// date.h ////////////
class Date
{
    // ...
    bool sameMonth( string aMonth, string aMonth2 ) const ; // *** const added
};

//////// date.cpp ///////////
#include "date.h"

bool Date::sameMonth( string aMonth, string aMonth2 ) const // *** note the Date::
{ return aMonth == aMonth2 ; }

Or (better):

////////// date.h ////////////
class Date
{
    // ...
    static bool sameMonth( string aMonth, string aMonth2 ) ; // *** static added
};

//////// date.cpp ///////////
#include "date.h"

bool Date::sameMonth( string aMonth, string aMonth2 )
{ return aMonth == aMonth2 ; }

Consider passing aMonth and aMonth2 by reference to const.
http://stackoverflow.com/questions/270408/is-it-better-in-c-to-pass-by-value-or-pass-by-constant-reference

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.