Write a complete C++ program that implements this Date class (written below), wi
ID: 3542388 • Letter: W
Question
Write a complete C++ program that implements this Date class (written below), with the modifications and improvements outlined below. DO NOT SOLICIT services--if you cannot answer the question, do not respond I will report you._________________________________________________________________________________________________
#include <iostream>
#include <cmath>
using namespace std;
class Date
{ public:
Date (int = 1, int = 1, int = 1990); //default constructor with parameters
void print( ) const;
private:
int month;
int day;
int year;
};
Date :: Date (int m, int d, int y)
{
month = m; //note no error checking is performed at all Write a complete C++ program that implements this Date class (written below), with the modifications and improvements outlined below. DO NOT SOLICIT services--if you cannot answer the question, do not respond I will report you.
_________________________________________________________________________________________________
#include <iostream>
#include <cmath>
using namespace std;
class Date
{ public:
Date (int = 1, int = 1, int = 1990); //default constructor with parameters
void print( ) const;
private:
int month;
int day;
int year;
};
Date :: Date (int m, int d, int y)
{
month = m; //note no error checking is performed at all
Explanation / Answer
here is the code:-
#include<iostream>
#include<cstring>
using namespace std;
class date{
int month,day,year;
bool leapyear(int);
bool end_of_month(int);
static const int date1[];
public:
void setdate(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
date()
{
month=1;
day=1;
year=1990;
}
void nextday();
void preday();
void afteradd(int);
void compare(date,date);
friend istream& operator>>(istream &,date &);
friend ostream& operator<<(ostream &,date &);
};
void printopt();
int getopt();
const int date::date1[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
istream& operator>>(istream &i,date &i1)
{
int y,m,d;
cout<<"enter date in m:d:y"<<endl;
i>>m>>d>>y;
i1.setdate(m,d,y);
}
ostream& operator<<(ostream &i,date &i1)
{
int y,m,d;
cout<<"date in m:d:y is"<<endl;
i<<i1.month<<":"<<i1.day<<":"<<i1.year;
return i;
}
void date::nextday()
{
if(month>13)
cout<<"invalid month"<<endl;
else if(!end_of_month(day))
{
day++;
cout<<month<<":"<<day<<":"<<year<<endl;
}
else if(month<=12 && day<date1[month])
{
day++;
cout<<"year"<<year<<"month"<<month<<"day"<<day<<endl;
}
else if(day==date1[month]&& month==12)
{
month=1;
day=1;
year++;
cout<<"year"<<year<<"month"<<month<<"day"<<day<<endl;
}
else if(day==date1[month])
{
month++;
day=1;
cout<<"year"<<year<<"month"<<month<<"day"<<day<<endl;
}
else
{
year++;
month=1;
day=1;
cout<<"year"<<year<<"month"<<month<<"day"<<day<<endl;
}
}
bool date::end_of_month(int tday)
{
if(month==2 && leapyear(year))
return tday=29;
else
return tday;
}
bool date::leapyear(int year)
{
if(year%4==0)
return 1;
else
return 0;
}
void date::preday()
{
if(day!=1)
{
day--;
cout<<"year"<<year<<"month"<<month<<"day"<<day<<endl;
}
else if(month==1 && day==1)
{
year--;
month=12;
day=31;
cout<<"year"<<year<<"month"<<month<<"day"<<day<<endl;
}
else if(month==3 && day==1 && leapyear(year))
{
day=29;
month--;
cout<<"year"<<year<<"month"<<month<<"day"<<day<<endl;
}
else
{
month--;
day=date1[month];
cout<<"year"<<year<<"month"<<month<<"day"<<day<<endl;
}
}
void date::afteradd(int a)
{
for(int i=0;i<a;i++)
nextday();
}
void date::compare(date d1,date d2)
{
if(d1.year==d2.year && d1.month==d2.month && d1.day== d2.day)
cout<<"date's are equal"<<endl;
else
cout<<"date's are not equal"<<endl;
}
int main()
{
date d1,d2;
int year,month,day;
int opt,x;
while(1)
{
printopt();
opt=getopt();
switch(opt)
{
case 0:exit(0);
case 1:
d1.nextday();
break;
case 2:d1.preday();
break;
case 3:d1.compare(d1,d2);
break;
case 4:cout<<"enter value to inc date:"<<endl;
cin>>x;
d1.afteradd(x);
break;
case 5:cin>>d1;
cin>>d2;
break;
case 6:cout<<d1<<endl;
cout<<d2<<endl;
break;
}
}
return 0;
}
void printopt()
{
cout<<"0.exit"<<endl;
cout<<"1.next"<<endl;
cout<<"2.pre"<<endl;
cout<<"3.compare"<<endl;
cout<<"4.afteradd"<<endl;
cout<<"5.read"<<endl;
cout<<"6.print"<<endl;
}
int getopt()
{
int opt;
cout<<"enter for option"<<endl;
cin>>opt;
return opt;
}
the above is compiled without any errors and gives the requied ouput.............
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.