Stay Tuned!

Subscribe to our newsletter to get our newest articles instantly!

C++ Programming

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

Pre-increment and Post-increment

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

Avatar

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.

You may also like

program to find leap year
C++ Programming

C++ Program to Find Leap Year by Using Function

  • March 3, 2016
Write a program to find if the inputted year is a leap year or not by using the concept of