Create your own date class.
Example run
Please type in a month (1 - 12): 1
Please type in a day (1 - 31): 28
Please type in a year: 2020
Long Date: January 28, 2020
Short Date: 1/28/2020
Month: 1
Month Name: January
Day: 28
Year: 2020
Is Leap Year: Yes
Days in the Month: 31
Season: Winter
Code
#include<iostream>
using namespace std;
class MyDate
{
int month,day,year;
string yrs[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
public:
MyDate(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
bool isleap()
{
if(year%400==0)
return true;
else if(year%100==0)
return false;
else if(year%4==0)
return true;
return false;
}
string shortformat()
{
string s="";
s=s+to_string(month)+'/'+to_string(day)+'/'+to_string(year);
return s;
}
string longformat()
{
string s="";
s=s+yrs[month-1]+' '+to_string(day)+", "+to_string(year);
return s;
}
int getday()
{
return day;
}
int getmonth()
{
return month;
}
string getmonthname()
{
return yrs[month-1];
}
int getyear()
{
return year;
}
int daysinmonth()
{
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
return 31;
else if(month==2)
{
if(isleap())
return 29;
else
return 28;
}
return 30;
}
string season()
{
if(month==3 || month==4 || month==5)
return "Spring";
if(month==6 || month==7 || month==8)
return "Summer";
if(month==9 || month==10 || month==11)
return "Fall";
return "Winter";
}
};
int main()
{
int m,d,y;
cout<<"Please type in a month (1 - 12): ";
cin>>m; //input month
cout<<"Please type in a day (1 - 31): ";
cin>>d; //int date
cout<<"Please type in a year: ";
cin>>y; //int year
MyDate ob(m,d,y); //create object and call the constructor
cout<<"Long Date: "<<ob.longformat();
cout<<"\nShort Date: "<<ob.shortformat();
cout<<"\nMonth: "<<ob.getmonth();
cout<<"\nMonth Name: "<<ob.getmonthname();
cout<<"\nDay: "<<ob.getday();
cout<<"\nYear: "<<ob.getyear();
cout<<"\nIs Leap Year: "<<ob.isleap();
cout<<"\nDays in the Month: "<<ob.daysinmonth();
cout<<"\nSeason: "<<ob.season();
return 0;
}
Terminal Work
.
Get Answers For Free
Most questions answered within 1 hours.