Write a C++ program to overload the + operator to increment the date object.Sample input23 //year10//month8//daySample output24 11 9
Question
Write a C++ program to overload the + operator to increment the date object.
Sample input
23
//year
10
//month
8
//day
Sample output
24 11 9
Solution
Sure, here is a simple C++ program that overloads the + operator to increment the date object.
#include<iostream>
using namespace std;
class Date {
int day, month, year;
public:
Date(int d, int m, int y) {
day = d;
month = m;
year = y;
}
void operator ++() {
day++;
if (day > 30) {
month++;
day = 1;
}
if (month > 12) {
year++;
month = 1;
}
}
void displayDate() {
cout << "Year: " << year << " Month: " << month << " Day: " << day << endl;
}
};
int main() {
Date date1(8, 10, 23);
++date1;
date1.displayDate();
return 0;
}
This program defines a class Date
with three data members: day
, month
, and year
. The constructor Date(int, int, int)
initializes these data members. The operator++
function is overloaded to increment the date. If the day exceeds 30, it is reset to 1 and the month is incremented. If the month exceeds 12, it is reset to 1 and the year is incremented. The displayDate()
function is used to display the date. In the main()
function, a Date
object is created and incremented using the overloaded ++
operator. The date is then displayed.
Similar Questions
Write a C++ program to overload the + operator to increment the date object.Sample input23 //year10//month8//daySample output24 11 9
Explain how to convert a number of days to a fractional part of a year.Using the ordinary method, divide the number of days by
Write a program overloading arithmetic operators to add two complex numbers using oops c++
To convert the above string, what should be written in place of date_format?“%d/%m/%y”“%D/%M/%Y”“%d/%M/%y”“%d/%m/%
ind the difference between two dates in terms of number of days.input2021-05-012021-05-16
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.