Programming language is C++. Show finished code and an example of the output on
ID: 3825774 • Letter: P
Question
Programming language is C++. Show finished code and an example of the output on PuTTY when done. Use g++ -std=c++17 to compile the code with the chrono.cpp and chrono.h file on PuTTY.
3. (20 points) Start with Chrono,h and Chrono.cpp in section 9.8; for now put a dummy "return Sunday; or "return d in the last 3 functions on pages 337-338. Add a prefix operator to the Date class by putting the declaration in the Date class in Chrono,h and the definition in Chrono.cpp. The prefix operator should change a date to "tomorrow" using the following pseudocode: Date& operator ++0{ /addlto d //tomorrow, unless we were at the end ofthe month //if is date is false //need to change to first of next month set d to 1 if m is December Ineed to change to next year too set m to January increment y else increment m return thisExplanation / Answer
main.cpp
===================================================
#include "chrono.h"
#include<iostream.h>
void main()
{
int d,m,y;
char ch;
while(ch =='y' || ch =='Y')
{
cout<<"Enter day Month Year";
cin>>d>>m>>y;
Date oDate1(d,m,y),oDate2;
oDate2 = ++oDate1;
cout<<"Incremented Date"<<oDate2.dd<<oDate2.mm<<oDate2.yy;
cout <<"press y to continue..";
cin>> ch;
}
}
chrono.h
===============================================================
class Date
{
public:
int mm,dd,yy;
bool validDate();
int daysOfMonth[12];
public:
Date();
Date(int d,int m, int y);
Date& operator++();
};
chrono.cpp
===============================================================
#include"chrono.h"
Date::Date(int d,int m, int y)
{
dd = d;
mm = m;
yy = y;
daysOfMonth[0]=31;
daysOfMonth[1]=28;
daysOfMonth[2]=31;
daysOfMonth[3]=30;
daysOfMonth[4]=31;
daysOfMonth[5]=30;
daysOfMonth[6]=31;
daysOfMonth[7]=31;
daysOfMonth[8]=30;
daysOfMonth[9]=31;
daysOfMonth[10]=30;
daysOfMonth[11]=31;
}
Date::Date()
{
}
bool Date::validDate()
{
if(dd>31 || mm>12 || dd<=0 ||mm <=0)
{
return false;
}
return true;
}
Date& Date::operator++()
{
int nd,nm,ny,ndays;
ndays=daysOfMonth[mm-1];
if(!validDate())// If Date Invalid 1st of next month
{
dd = 1;
mm++;
if(mm>12)
{
mm=1;
yy++;
}
}
if(mm==2)
{
if(yy%100==0)
{
if(yy%400==0)
ndays=29;
}
else if(yy%4==0)
ndays=29;
}
nd=dd+1;
nm=mm;
ny=yy;
if(nd>ndays)
{
nd=1;
nm++;
}
if(nm>12)
{
nm=1;
ny++;
}
return Date(nd,nm,ny);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.