Q BgQuestion:

      
Novice
Karma Points: 25
(North Carolina Central University)
Respect (73%):
posted by  Bamzy on 10/28/2009 5:55:28 PM  |  status: Closed  |  Earned Karma: 25

C++

Course Textbook Chapter Problem Needs by
Data Structures C++: How to Program (6th) by Deitel, Deitel cahpter 3 3.15 11/2/2009 at 12:00:00 PM
Question Details:

(Date Class) Create a class called Date that includes three pieces of information as data membersa month (type int), a day (type int) and a year (type int). Your class should have a constructor with three parameters that uses the parameters to initialize the three data members. For the purpose of this exercise, assume that the values provided for the year and day are correct, but ensure that the month value is in the range 1-12; if it is not, set the month to 1. Provide a set and a get function for each data member. Provide a member function displayDate that displays the month, day and year separated by forward slashes (/). Write a test program that demonstrates class Date's capabilities.

 Ensure the day is in the valid range based on the month and year information.

for example if date is 31 it would not be vaild for febuary because feubary has 28 or 29 days. Make it a valid date for what ever is entered.

bamzy
Bonus Point Alert! Earn +7 additional karma points for helping this gold member.

AAnswers:

Answer Question Ask for clarification
Oracle
Karma Points: 31,880
posted by rapunzel on 10/28/2009 9:17:57 PM  |  status: Live
Asker's Rating: Lifesaver   
Response Details:
please rate - thanks

hope this gets you started

#include <iostream>
using namespace std;
bool validdate(int &,int,int);
int leap(int);
class Date
{

private: int month,day,year;

public:
Date(int m, int d, int y)
{
month=m;
day=d;
year=y;}
void Date::printdate()
{
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
 int Date::getmonth()
{
return month;
}
 int getday()
{
return day;
}
 int Date::getyear()
{
return year;
}
 void Date::setmonth(int m)
{month=m;}
 void Date::setday(int d)
{day=d;
}
 void Date::setyear(int y)
{year=y;
}
};
int main()
{
int m,d,y,v;


cout<<"Enter month: ";
cin>>m;
cout<<"Enter day: ";
cin>>d;
cout<<"Enter year: ";
cin>>y;
cout<<endl;
v=validdate(d,m,y);
if(!v)  
   cout<<"invalid date, day "<<d <<" used\n";
Date dd(m,d,y);
dd.printdate();
d=dd.getday();
cout<<"day="<<d<<endl;

system("pause");
return 0;
}
bool validdate(int &day, int month,int year)
{int l;
    if (day <= 0)
        return false;

    switch(month)
    {
        case 1 :
        case 3 :
        case 5 :
        case 7 :
        case 8 :
        case 10:
        case 12: if (day > 31)
                    {day=31;
                    return false;
                     }
                  else
                     return true;
        case 4 :
        case 6 :
        case 9 :
        case 11: if (day > 31)
                    {day=31;
                    return false;
                     }
                  else
                     return true;
        case 2 : l=leap(year);
                if (day > 28+l)
                  {day=28+l;
                    return false;
                     }
                  else
                     return true;
    }
}
int leap(int year)
{int leapcode=0;
    if(year%4==0)
      {if(year%100!=0)
            leapcode=1;
       else
           if(year%400==0)
               leapcode=1;
       }
 return leapcode;
}



Note to all members 1. Please 1 question per post 2. Show respect to your fellow members by rating all answers. 3. When rating remember that the first answer is not necessarily the best answer. 4. When answering questions, explain what you are doing, so that the asker will learn, don't just give a meaningless number. 5. Your answers should be your work. Don't copy from another member, this is Karma abuse and possible disciplinary actions against you. and GOOD LUCK to you all!!
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »