Write a program overloading unary operator to increment date using oops in c++
Question
Write a program overloading unary operator to increment date using oops in c++
Solution
Sure, here is a simple example of a C++ program that overloads the unary operator ++ to increment the date:
#include<iostream>
using namespace std;
class Date {
int day, month, year;
public:
Date(int d=1, int m=1, int y=2000): day(d), month(m), year(y) {}
void displayDate() {
cout << "Date: " << day << "/" << month << "/" << year << endl;
}
// Overload unary ++ operator
Date operator++() {
day = day + 1;
if(day > 30) {
month++;
day = 1;
}
if(month > 12) {
year++;
month = 1;
}
return Date(day, month, year);
}
};
int main() {
Date date1(31, 12, 2021);
date1.displayDate();
++date1;
date1.displayDate();
return 0;
}
In this program, we have a Date class with a constructor to initialize the date, a displayDate function to print the date, and an overloaded operator++ function to increment the date.
In the operator++ function, we increment the day by 1. If the day exceeds 30, we increment the month by 1 and reset the day to 1. If the month exceeds 12, we increment the year by 1 and reset the month to 1.
In the main function, we create a Date object date1 with the date 31/12/2021, display the date, increment the date using the overloaded ++ operator, and display the date again. The output will be:
Date: 31/12/2021
Date: 1/1/2022
Please note that this is a simple example and does not take into account leap years or the different number of days in different months.
Similar Questions
unary operator in c language
Write a C++ program to overload the + operator to increment the date object.Sample input23 //year10//month8//daySample output24 11 9
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
Test time left: 02:46:08Understanding Unary operatorsWhat is the output of the code given below?#include <stdio.h>int main() { int i = 2; i = 3 + 3 * i++; printf("%d", i); return 0;}OptionsCompilation error12189
Single File Programming QuestionProblem StatementBrian is an enthusiastic learner diving into the world of programming! Brian is experimenting with unary operators and wants to understand their impact on a given variable.Can you help him by writing a program that takes an integer as input, performs a series of unary operations a = ++a * --a, and then displays the final value?
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.