C++ Programming

C++ Program to Implement Pre-increment and Post-increment Operators

Write a class date that uses pre-increment and post-increment operators to add 1 to the day in the date object, while causing appropriate increments to the month and year(use the appropriate condition for leap year).The pre and post increment operators in your date class should behave as the build in increment operators.

Source Code

#include<iostream>
#include<conio.h>

using namespace std;
class date
{

int year;
int mnth;
int day;
public:
void update()

{
int flag=0;
if((year%4==0)&&(((year%100)!=0)||((year%400)==0)))
flag=1;
if((((mnth==1)||(mnth==3)||(mnth==5)||(mnth==7)||(mnth==8)||(mnth==10)||(mnth==12))&&(day>31))||
((flag==1)&&(mnth==2)&&(day>29))||((flag==0)&&(mnth==2)&&(day>28))||
(((mnth==9)||(mnth==4)||(mnth==6)||(mnth==11))&&(day>30)))

{
mnth=mnth+1;
day=1;
}

if(mnth>12)
{
mnth=1;
year=year+1;
}

}

void operator ++()

{

day++;

}

void display()

{

update();

cout<<"Date is :"<<year<<"/"<<mnth<<"/"<<day;

}

void getdata()

{

cout<<"Enter Day:";

cin>>day;

cout<<"Enter Month:";

cin>>mnth;

cout<<"Enter Year:";

cin>>year;

}

};

int main()

{

date d1;

d1.getdata();

++d1;

d1.display();

}

Output

Back to top button