Thread: Homework Help - Create C++ Date class

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    27

    Homework Help - Create C++ Date class

    I'm new to C++, in fact, this is my second week with it. I'm having issues with the header and cpp files during compilation. I'll add one or two errors now, then some later if I absolutely can't get them goin'. I appreciate your help, guys. Thank you.

    Errors:
    In file included from Date_Hardesty.cpp:14:0:
    Date_Hardesty.h:22:13: error: ‘Date::Date(int, int, int)’ cannot be overloaded
    Date_Hardesty.h:20:13: error: with ‘Date::Date(int, int, int)’
    Date_Hardesty.cpp: In member function ‘void Date::setYear(int)’:
    Date_Hardesty.cpp:53:4: error: ‘else’ without a previous ‘if’

    This is my header file:

    Code:
    /*Line 11.*/ #include <string>
    //Line 12. prevent multiple inclusions of header
    /*Line 13.*/ #ifndef DATE_H
    /* Line 14.*/ #define DATE_H
    
    //Define Date class
    class Date
    {
    public:
    /*Line 20.*/   explicit Date (int = 1, int = 1, int = 2000);  /* my default constructor
                                                         sets date to Jan 1, 2000 */ 
       explicit Date (int month, int day, int year);  //my 3-arg constructor 
       void setMonth (int);      //function sets the Month
       void setDay (int);        //function sets the Day
       void setYear (int);       //function sets the Year
       int getMonth ();      //the function to return the int Month
       int getDay ();        //the function to return the int Day
       int getYear ();       //the function to return the int Year
       std::string getMonthName (int);  //to receive the string name of the  Month
       void print ();            //function to output Date as MM/DD/YYYY
       void printLong ();        //function to output Date as DD MonthName YYYY
       ~Date ();                //makin' sure everything gets destructed in order
    
    private: 
       int month, day, year;
       
    };  //end the class Date 
    
    #endif
    And, this is my Date_Hardesty.cpp file:

    Code:
    //Line 47. start function to set the Year according to the assignment's parameters
    void Date::setYear (int y) 
    {
       if (y < 1900)
          y = 1;
          year = y;
       else
          year = y; 
    }  //end setYear
    Last edited by boygenuis; 05-31-2014 at 02:23 PM. Reason: Disabled smiles

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,602
    You can't overload two functions with the same type and number of parameters.
    Code:
    /*Line 20.*/   explicit Date (int = 1, int = 1, int = 2000);  /* my default constructor
                                                         sets date to Jan 1, 2000 */
       explicit Date (int month, int day, int year);  //my 3-arg constructor
    Really your "default" constructor should have no parameters and use an initialization list to assign the "default" values to the variables.

    Code:
    Date() : month(1), day(1), year(2000) {}
    For the last error look at this snippet:
    Code:
    void Date::setYear (int y)
    {
       if (y < 1900)
          y = 1;
          year = y;
       else
    If you don't use braces in your if statements only one statement will be included in that if statement.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    So, if I have this right, the if statement in my cpp file should look like:

    Code:
    void Date::setYear (int y)
    {
       if (y < 1900)
       {
          y = 1;
          year = y;
       }
       else
          year = y; }  //end setYear
    And, my Date class should look like:

    Code:
    #include <string>
    
    //prevent multiple inclusions of header
    
    #ifndef DATE_H
    
    #define DATE_H
    
     
    
    //Define Date class
    
    class Date
    
    {
    
    public:
       Date () : month(1), day(1), year (200) {};           /* my default constructor
    
                                                               sets date to Jan 1, 2000 */  
    
       //explicit Date (int month, int day, int year);  //my 3-arg constructor 
    
       void setMonth (int);      //function sets the Month
    
       void setDay (int);        //function sets the Day
       void setYear (int);       //function sets the Year
       int getMonth ();      //the function to return the int Month
       int getDay ();        //the function to return the int Day
       int getYear ();       //the function to return the int Year
       std::string getMonthName (int);  //to receive the string name of the  Month
       void print ();            //function to output Date as MM/DD/YYYY
       void printLong ();        //function to output Date as DD MonthName YYYY
       ~Date ();                //makin' sure everything gets destructed in order
     
    private: 
       int month, day, year;
        
    };  //end the class Date 
     
    #endif

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,602
    There is no reason you can't also have the three argument constructor as well. Or any of other unique constructors that may be required for the proper operation of your class.

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    A-ha. So this would also be cool?

    Code:
    public:
       explicit Date () : month(1), day(1), year (200) {};           /* my default constructor
    
                                                               sets date to Jan 1, 2000 */
    
       explicit Date (int month, int day, int year);  //my 3-arg constructor
    This is great, thanks. I'll see if I can't figure the other errors out, then maybe ask more again.
    Last edited by boygenuis; 05-31-2014 at 04:17 PM. Reason: forgot my explicit operator & thanks

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The ending brace for your code should go on a separate line. It helps code readability.
    Your functions in your class definition should have parameter names; otherwise it is hard to know what arguments they take.
    For example, if you say
    explicit Date (int, int, int); //my 3-arg constructor
    Then what parameters does it take? Presumably year, month and day, but in what order? If you follow the ISO standard, then it's Year, Month, Day. If you follow the reverse order, it's Day, Month, Year. If you follow the American standard, it may be Year, Day, Month.
    Well, you see the problem. That's why you should always use descriptive names for parameters. See more at this linky.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by boygenuis View Post
    A-ha. So this would also be cool?

    Code:
    public:
       explicit Date () : month(1), day(1), year (200) {};           /* my default constructor
    
                                                               sets date to Jan 1, 2000 */
    
       explicit Date (int month, int day, int year);  //my 3-arg constructor
    It's permitted, yeah. It's coolness or otherwise depends on what you want to achieve. And whether you fix the typo or not

    The first form you had (with three default values) can be invoked with zero, one, two, or three arguments. So with a single constructor of the form
    Code:
        explicit Date (int = 1, int = 1, int = 2000);
    can be invoked in any of the following forms;
    Code:
        Date d0;                  //   equivalent to Date d0(1,1,2000)
        Date d1(5);             //    equivalent to Date d1(5,1,2000)
        Date d2(5,6);          //    equivalent to Date d2(5,6,2000)
        Date d3(5,6,1984);  //    equivalent to Date d2(5,6,1984)
    All of the above call the same constructor.

    With your two-constructors (one that accepts three arguments, one that accepts no arguments) you can only invoke the 0 argument and 3 argument forms. They are distinct constructors. The 1 and 2 argument forms are not permitted (unless you define them as well).


    There is a trade-off that, with default argument values, the default arguments have to be specified in the class definition (or, more accurately, the declaration of the constructor within the class definition). If you need to change the default values for any reason, it is necessary to change the class definition (which will force the compiler to recompile every source file which uses your Date class). That can be a significant concern for large projects (for example, if you have a few hundred source files that #include <date.h>, all of them will be recompiled, whether they use the constructors or not).

    With your second form (a non-argument constructor and a three-argument constructor) it is not necessary to place the default values in the class definition. If you separate the declaration from implementation of the constructor (e.g. declare the constructor in the header, implement it in date.cc) you can change the default values, without forcing a recompile.

    Neither approach is more "cool" than the other .... the choice depends on what you are trying to achieve.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    Thank you, all, for the help. Alas, I have two more errors which I've been unable to resolve.

    Errors:
    Date_Hardesty.cpp: In member function ‘std::string Date::getMonthName(int)’:
    Date_Hardesty.cpp:81:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:83:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:85:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:87:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:89:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:91:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:93:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:95:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:97:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:99:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:101:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:103:7: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp:105:11: error: ‘name’ was not declared in this scope
    Date_Hardesty.cpp: In member function ‘void Date:rintLong()’:
    Date_Hardesty.cpp:118:73: error: expected primary-expression before ‘int’
    DateDemo.cpp: In function ‘int main()’:
    DateDemo.cpp:38:37: error: expected primary-expression before ‘int’

    That same ole header file:
    Code:
    #include <string>
    //prevent multiple inclusions of header
    #ifndef DATE_H
    #define DATE_H
    
    //Define Date class
    class Date
    {
    public:
       explicit Date () : month (1), day (1), year (2000)  // my default
       {                                                   // constructor
                                                           // sets date to
       };                                                  // Jan 1, 2000
       explicit Date (int month, int day, int year);  //my 3-arg constructor 
       void setMonth (int);      //function sets the Month
       void setDay (int);        //function sets the Day
       void setYear (int);       //function sets the Year
       int getMonth ();          //the function to return the int Month
       int getDay ();            //the function to return the int Day
       int getYear ();           //the function to return the int Year
       std::string getMonthName (int);  //to receive the string name of the  Month
       void print ();            //function to output Date as MM/DD/YYYY
       void printLong ();        //function to output Date as DD MonthName YYYY
       ~Date ();                //makin' sure everything gets destructed in order
    
    private: 
       int month, day, year;
       
    };  //end the class Date 
    
    #endif
    excerpts from my Date_Hardesty.cpp file
    Code:
    //**LINE 77**  function to return the MonthName to main
    std::string Date::getMonthName (int m)
    {
       if (m = 1)
          name = "January";
       if (m = 2)
          name = "February";
       if (m = 3)
          name = "March";
       if (m = 4)
          name = "April";
       if (m = 5)
          name = "May";
       if (m = 6)
          name = "June";
       if (m = 7)
          name = "July";
       if (m = 8)
          name = "August";
       if (m = 9)
          name = "September";
       if (m = 10)
          name = "October";
       if (m = 11)
          name = "November";
       if (m = 12)
          name = "December";
    
       return name;  //send the Month's name back to main
    } //end getMonthName
    So, I tried adding a string "name" to the "private" area of my header, but that gave me the same errors. I tried adding "name" to my cpp file with my main function, but that also did not work. Clearly I should be initializing "name" somewhere else, but I don't know where.

    Then, the errors about, "expected primary expression before 'int'" confuses me.

    Code:
    //**LINE 38** start function to set the Day according to the assignment's parameters
    void Date::setDay(int d)  
    { 
       if (d >= 1 && d <= 31)
          day = d;
       else
          day = 1;
    }  //end setDay
    
    ...
    some more code
    ...
    
    //**LINE 115** function to format the DD Month YYYY printLong function
    void Date::printLong ()
    {
        cout << setfill ('0') << setw (2) << getDay()  << " " << getMonthName(int) << " " << getYear()  << "\n";
    
    }  //end printLong function
    Your thoughts?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,602
    Clearly I should be initializing "name" somewhere else, but I don't know where.
    How about in the function where it is used, before you use it?
    Code:
    std::string Date::getMonthName (int m)
    {
       std::string name = "Blank";
       ...
    Also look at this snippet:
    Code:
        cout << setfill ('0') << setw (2) << getDay()  << " " << getMonthName(int)
    You need a variable name or a constant value in that last function call, not a type of variable.

    Look at the following snippet:
    Code:
    void Date::setDay(int d) 
    {
       if (d >= 1 && d <= 31)
          day = d;
       else
          day = 1;
    }
    What happens if the month happens to be Febuary? Or June, or any other month that has less than 31 days?


    Jim

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>Date_Hardesty.cpp:81:7: error: ‘name’ was not declared in this scope
    Like it says, you have not declared a variable name in the current scope. Clearly, it's a local variable because its usefulness ends when the function ends, so declare it in your function.

    >>Date_Hardesty.cpp:118:73: error: expected primary-expression before ‘int’
    cout << setfill ('0') << setw (2) << getDay() << " " << getMonthName(int) << " " << getYear() << "\n";
    What is that int supposed to do?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    Declare it in the function itself!!! I should have seen that.

    Thank you for catching the setDay error. I crossed it off my list, but the function wasn't finished. I'll revisit that tomorrow afternoon.

    I'm still having issues with the
    DateDemo.cpp: In function ‘int main()’:
    DateDemo.cpp:38:37: error: expected primary-expression before ‘int’

    Even though line 38 is commented. I are confoosed.

    Code:
    //**LINE 38** start function to set the Day according to the assignment's parameters
    void Date::setDay (int d)  
    { 
       if (d >= 1 && d <= 31)
          day = d;
       else
          day = 1;
    }  //end setDay

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    since you didn't post your main function, it's impossible for us to help you with that.

    another thing that no one else seems to have commented on yet is the fact that getMonthName() will always return "December"

    note that there is a difference between "=" and "=="
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #13
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    Sorry, got confused. Thanks for checking my comparisons in getMonthName

    Here's my main function.

    Code:
    // DateDemo.cpp
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    using std::cout;
    using std::endl;
    using std::setfill;
    using std::setw;
    using std::string;
    
    #include "Date_Hardesty.h"
    
    // note - you may need to change the definition of the main function to
    // be consistent with what your C++ compiler expects.
    
    //int _tmain(int argc, _TCHAR* argv[])
    int main()
    {
        Date d1;             // default ctor
        Date d2(7, 4, 1976); // July 4'th 1976
        Date d3(0, 15, 1880);// Adjusted by ctor to January 15'th 1900
    
        d1.print();         // prints 01/01/2000
        d1.printLong();     // prints 1 January 2000
        cout << endl;
    
        d2.print();         // prints 07/04/1976
        d2.printLong();     // prints 4 July 1976
        cout << endl;
    
        d3.print();         // prints 01/15/1900
        d3.printLong();     // prints 15 January 1900
        cout << endl;
    
        cout << "object d2's day is " << d2.getDay() << endl;
        cout << "object d2's month is "  << d2.getMonth() << 
        " which is " << d2.getMonthName(int) << endl;
        cout << "object d2's year is " << d2.getYear() << endl;
    
        return 0;
    }

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,602
    Please re-read posts #9 and #10.

    Jim

  15. #15
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    But if I declare a variable "name" or "month" or whatever in main, it won't contain whatever information from my Date_Hardesty.cpp or Date_Hardesty.h files. How would I get that variable in main to have this info?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help: program to graph date...
    By Jazmin Jasso in forum C Programming
    Replies: 5
    Last Post: 04-29-2013, 08:55 PM
  2. Replies: 1
    Last Post: 05-07-2012, 11:39 PM
  3. Create struct tm from user specified date
    By cunzlow in forum C Programming
    Replies: 4
    Last Post: 09-30-2009, 12:39 PM
  4. Retreiving File create/modify date
    By super_stripey in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2004, 07:34 AM
  5. date class
    By aquaman in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2001, 09:57 AM

Tags for this Thread