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 input23 //year10//month8//daySample output24 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 that prompts the user to enter the date as three integer values for the month, the day in the month, and the year. The program should then output the date in the form 31st December 2010 when the user enters 12 31 2010, say. The program has to work out when superscripts “th”, “nd”, “st”, and “rd” need to be appended to the day value. The programmer should not forget 1st, 2nd, 3rd, 4th; and then 11th, 12th, 13th, 14th; and 21st, 22nd, 23rd, and 24th.Testcases:Input:02 11 2021Output:11th February 202
Write a program overloading unary operator to increment date using oops in c++
create a class called date, with integer data members for day, month, and year. the comprises of member functions: 1. to display date in DD/MM/YYYY format 2. to subtract an integer from date object 3. to subtract one date from another using oops in c++
shu wants to create a program that asks users to input their birthdates, including the day, month, and year. The program should then display the entered birthdate in the format "DD/MM/YYYY."Write a program to assist Ishu in achieving this task.Input format :The input consists of three space-separated integer values, representing the user's birthdate(day, month, and year).Output format :The output displays the user's birthdate in the format "Your birthday is on " followed by the formatted date DD/MM/YYYY.
Create a class called Date that includes three pieces of information as instance variables—a month, a day and a year. Your class should have a constructor that initializes the three instance variables after validating the given date. Provide a getDetails() member method to read the date. Provide a method displayDate() that displays the month, day and year separated by forward slashes(/). Write a C++ application and pseudocode that demonstrates classDate’s capabilities.Input : Read Month, Day and Year (Line by line)Output :Print Date that displays the month, day and year separated by forward slashes(/)Note: print "Invalid Date" other than the following2000 < Year < 20250 < Day <= 31 ( for the months Jan, March, May, July, August, October, December)0 < Day <= 30 ( for the months April, June, September, and November)0 < Day <= 28 ( for the month February ) and (0 < Day <= 29), if month = 2 and year = leap year0 < Month < = 12
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.