Write a program to create a class Date with day, month and year as members. Write a member
function to validate the dates and display the invalid dates. Test the class with an array of Date objects.
// A C++ program to check if date is valid
#include<iostream>
using namespace std;
class date
{
private:
int day,month,yr;
public:
int validate();
};
int validate()
{
int day,month,yr;
cout<<"Enter date (In the order DAY MONTH YEAR, please) \n";
cin>>day>>month>>yr;
if(1000 <= yr <= 3000)
{
if((month==1 || month==3 || month==5|| month==7|| month==8||month==10||month==12) && day>0 && day<=31)
cout<<"That is a valid date\n\n";
else
if(month==4 || month==6 || month==9|| month==11 && day>0 && day<=30)
cout<<"That is a valid date\n\n";
else
if(month==2)
{
if((yr%400==0 || (yr%100!=0 && yr%4==0)) && day>0 && day<=29)
cout<<"That is a valid date\n\n";
else if(day>0 && day<=28)
cout<<"That is a valid date\n\n";
else
cout<<"That is an Invalid date\n\n";
}
else
cout<<"That is an Invalid date\n\n";
}
else
cout<<"That is an Invalid date\n\n";
}
int main()
{
date checkdate;
cout<<validate();
}
OUTPUT when the date is valid
OUTPUT when the date is Invalid
Comments
Sir, Why it is not answered
Leave a comment