Answer to Question #252241 in C++ for Flying Bird

Question #252241

2.     Using C++ Build a class Date with the following attributes

· Day(int)

· Month(int)

·  Year(int)

Provide a method setDate(int d, int mon, int y). Inside this method you will assign the value of d to the day attribute, mon to the month attribute and y to the year attribute.

Provide another method nextDay(). This method will increment the day to the next day but do implement sanity checks. i.e. if the day is already 31 then the next day will be 1 and you will increment month. Similarly if the day is 30 but the month is 4 (April) then the next day will again be 1 and the month will be incremented to 5. Similarly if the date is already 31 of December i.e. day is 31 and month is 12 then the next day will mean that day becomes 1, month also becomes 1 and the year is incremented. So look for all possible situations and implement this method accordingly

Provide a method print that prints the date in the following format 6-10-2021


1
Expert's answer
2021-10-16T13:53:13-0400
#include<iostream>
using namespace std;
class Date{
	int  Day;


int  Month;


int  Year;
void setDate(int d, int mon, int y){
	Day =d;
	Month = mon;
	Year = y;
}
void nextDay(){
	if(Day==31){
		Day= 1;
		Month++;
	}
	else if(Day==30 && Month==4){
		Day = 1;
		Month++;
	}
	else if(Day==31 && Month==12){
		Month = 1;
		Day = 1;
		Year ++;
	}
	else  if(Day != 31){
		Day++;
	}
}


void print(){
	cout<<Day<<" - "<<Month<<"- "<<Year<<endl;
}
	
};


int main(){
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS