Thread: Overloaded Constructors hw help

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Exclamation Overloaded Constructors hw help

    Problem Description

    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

    ---------------------------------------------
    This is what i have so far
    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 ];
    }
     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;
    }
    Error codes :
    ---32:2 C:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.

    ---- C:\Users\jowner\Desktop\Date.cpp In member function `void Date::convert1(int)':

    ----94 C:\Users\jowner\Desktop\Date.cpp name lookup of `m' changed for new ISO `for' scoping

    ----88 C:\Users\jowner\Desktop\Date.cpp using obsolete binding at `m'

    Please help, i cant get it to work

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Hmm, need to work a bit on the formatting of that code there, bit anyways...

    For the deprecated/antiquated header warning:
    <string.h> should be <cstring>
    <iostream.h> should be <iostream>
    <time.h> should be <ctime>

    There is also no need for the header file to be including any of those... at least as you've posted it. Changing those might bring a host of other (easily addressed) issues, namely the std:: namespace that you will need to deal with.

    As for the others:
    Code:
    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 );
    }
    I believe that what is being told to you is that the variable m was created within the scope of the for loop and that outside of said for loop m no longer exists. You should be able to easily resolve this by moving the declaration of m such that it is outside of (and prior to) the for loop.

    I have not done a more thorough examination of the code other than addressing your posted error messages.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >><iostream.h> should be <iostream>
    In fact, this is a requirement. There is no header <iostream.h> in the standard. Some compilers erroneously accepts them.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with overloaded constructors
    By bobknows in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2011, 10:36 AM
  2. Overloaded >>
    By alphaoide in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2004, 12:27 AM
  3. overloaded <<
    By alphaoide in forum C++ Programming
    Replies: 9
    Last Post: 03-15-2004, 08:29 PM
  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