Thread: C++: working with overloaded constructors

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    4

    C++: working with overloaded constructors

    I need help making my program like the requirement I need, including asking the user the prompt.
    What is a testing program? How does overloaded constructors work?
    Requirements
    1. 3 files: Date.h, Date.cpp -> class implementation, main.cpp -> testing program.

    Create a Date class with the following capabilities:
    a) Output the date in multiple formats such as
    MM/DD/YYYY
    June 14, 1992


    b) Use overloaded constructors to create Date objects initialized with dates of the formats in part a).

    You should only have 3 member data: integer month, integer day, and integer year. Also, you should have at least 2 constructors and 2 functions in your Date.cpp.
    Code:
    Date::Date ( int mm, int dd, int yy )
    {   // check mm, dd, and yy and save them into month, day, and year member data.
    
    }
     
    Date::Date ( string mstr, int dd, int yy )
    {   // check and convert mstr, dd, and yy and save them 
        //       into month, day, and year member data.
    
    }
     
    void Date::print ( )
    {   //  print the date in MM/DD/YYYY
    
    }
     
    void Date::printFullDate ( )
    {   //  print the date in long date such as
        //                June 24, 2002
    
    }
    You also need to create 3 get functions and 3 set functions for the member data.

    Sample Run:
    3 choices for the user to pick:
    Enter 1 for format: mm/dd/yyyy
    Enter 2 for format: month dd, yyyy
    Enter 3 to exit:

    Choice: 1 //user input

    //user inputs the information
    Enter month (1-12): 1
    Enter day of month: 5
    Enter year: 2005

    Mm/dd/yyyy: 1/5/2005 //gets checked for valid date.
    //////////////
    Enter 1 for format: mm/dd/yyyy
    Enter 2 for format: month dd, yyyy
    Enter 3 to exit:

    Choice:2
    Enter month name: May
    Enter day of month: 10
    Enter year:2010
    Month dd, yyyy: May 10, 2010

    Enter 1 for format: mm/dd/yyyy
    Enter 2 for format: month dd, yyyy
    Enter 3 to exit:

    Choice:3
    ///////////////
    Date.h
    Code:
    #ifndef DateFormat
    #define DateFormat
    #include <time.h>
    #include <string.h>
    
    class Date {
     public:
     Date();
     Date( int, int );
     Date( int, int, int );
     Date( char *, int, int );
     void setMonth( int );
     void setDay( int );
     void setYear( int );
     void printDateSlash( void ) const;
     void printDateMonth( void ) const;
     void printDateDay( void ) const;
     const char *monthName( void ) const;
     int leapYear( void ) const;
     int daysOfMonth( void ) const;
     void convert1( int );
     int convert2( void ) const;
     void convert3( const char * const );
     const char *monthList( int ) const;
     int days( int ) const;
    
     private:
     int day;
     int month;
     int year;
     };
    
     #endif
    
    Date.cpp
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <time.h>
    #include "Date.h"
    
    
    Date::Date()
    {
                struct tm *ptr; 
                time_t t = time( 0 ); 
                ptr = localtime( &t ); 
                day = ptr->tm_mday; 
                month = 1 + ptr->tm_mon;
                year = ptr->tm_year + 1900; 
    }
    
    
    Date::Date( int ddd, int yyyy )
    
    {
     setYear( yyyy );
     convert1( ddd ); 
    }
    
    
    Date::Date( int mm, int dd, int yy )
    {
    setYear( yy + 1900 );
    setMonth( mm );
    setDay( dd );
    }
    
    
    Date::Date( char *mPtr, int dd, int yyyy )
    {
    setYear( yyyy );
    convert3( mPtr );
    setDay( dd );
    }
    
    
    void Date::setDay( int d )
    { day = d >= 1 && d <= daysOfMonth() ? d : 1; }
    
    
    void Date::setMonth( int m ) { month = m >= 1 && m <= 12 ? m : 1; }
    
    
    void Date::setYear( int y ) { year = y >= 1900 && y <= 1999 ? y : 1900; }
    
    
    void Date::printDateSlash( void ) const
    { cout << month << '/' << day << '/' << year << '\n'; }
    
    
    void Date::printDateMonth( void ) const
    { cout << monthName() << ' ' << day << ", " << year << '\n'; }
    
    
    void Date::printDateDay( void ) const
    { cout << convert2() << ' ' << year << '\n'; }
    
    
    const char *Date::monthName( void ) const { return monthList( month - 1 ); }
    
    
    int Date::daysOfMonth( void ) const
    { return leapYear() && month == 2 ? 29 : days( month ); }
    
    
    int Date::leapYear( void ) const
    {
        if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
        return 1;
        else
        return 0;
    }
    
    
    void Date::convert1( int ddd ) 
    {
    int dayTotal = 0;
    
     if ( ddd < 1 || ddd > 366 ) 
     ddd = 1;
     setMonth( 1 );
    
               for ( int m = 1; m < 13 && ( dayTotal + daysOfMonth() ) < ddd; ++m ) {
               dayTotal += daysOfMonth();
               setMonth( m + 1 );
    }
    
     setDay( ddd - dayTotal );
     setMonth( m );
    }
    
    int Date::convert2( void ) const
    {
        int ddd = 0;
    
        for ( int m = 1; m < month; ++m )
        ddd += days( m );
    
        ddd += day;
        return ddd;
    }
    
    
    void Date::convert3( const char * const mPtr ) 
    {
      int flag = 0;
    
       for ( int subscript = 0; subscript < 12; ++subscript )
       if ( !strcmp( mPtr, monthList( subscript ) ) ) {
       setMonth( subscript + 1 );
       flag = 1; 
       break; 
    }
    
       if ( !flag )
       setMonth( 1 ); 
    }
    
    
    const char *Date::monthList( int mm ) const
    {
     char *months[] = { "January", "February", "March", "April", "May",
     "June", "July", "August", "September", "October",
     "November", "December" };
    
    return months[ mm ];
    }
    
    
    int Date::days( int m ) const
    
    
    {
    const int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
    return monthDays[ m - 1 ];
    }
    main.cpp
    Code:
    int main()
    {
     Date d1( 7, 4, 98 ), d2( 86, 1999 ),
     d3, d4( "September", 1, 1998 );
    
     d1.printDateSlash(); 
     d2.printDateSlash();
     d3.printDateSlash();
     d4.printDateSlash();
     cout << '\n';
    
     d1.printDateDay(); 
     d2.printDateDay();
     d3.printDateDay();
     d4.printDateDay();
     cout << '\n';
    
     d1.printDateMonth(); 
     d2.printDateMonth();
     d3.printDateMonth();
     d4.printDateMonth();
     cout << endl;
    
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloaded Constructors hw help
    By 123456tacos in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2011, 03:55 AM
  2. help with overloaded constructors
    By bobknows in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2011, 10:36 AM
  3. Overloaded >>
    By alphaoide in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2004, 12:27 AM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  5. using overloaded constructors
    By Alicia in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 04:36 PM

Tags for this Thread