Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is the code in C++ i want to let the user be able to enter new date and whenever he enters month more than

this is the code in C++ i want to let the user be able to enter new date and whenever he enters month more than 12 like 13 or zero or negative it will default to 1 and display the new date result

#include #include using namespace std; // creating a class named Date with three parameters on month, day and year , all are integer types class Date { //private access specifier for those integers private: int month, day, year; //public access specifiers with constructor to initialize the previously mentioned parameters public: // Constructor must be same name as the class and with no retuen or return type with three parameters, adn isnide the constructor we must put the if statement for the month Date(int m, int d, int y) { month = m; day = d; year = y; // Ensure that the month value is in the range 1-12 if (month < 1 || month > 12) { month = 1; //if the date is set above 12, it will automatically set it to 1 } } //For the purpose of this exercise, assume that the values provided for the year and day are correct // setter member function for the integer day void setDay (int d) { day = d; } //getter memebr functionn for the integer day int getDay() { return day; } //setter memebr function for integer year data member void setYear(int y) { year = y; } //getter member function for integer year data memebr int getYear() { return year; } // setter function for month integer data member void setMonth(int m) { month = m; } // getter function for month integer data member int getMonth() { return month; } // Member function to display the date in the format: month/day/year //idea was refrenced from w3schools.com void displayDate() { cout << month << "/" << day << "/" << year << endl; //formatting the output using "/" } };

int main() { int day, month, year; // Create a Date object with the constructor and initialize the data members Date today(2, 3, 2023); // Display the date cout << "Today's date is: "; today.displayDate(); }

all you need to do is add few extra steps where the main function is, to let the user enter new day and month and year , and whenever the month entered is not between 12 and 1 it will default the month to 1

the whole purpose behind this is to test the class capabilities focusing on entering wrong month and defaulting it to 1 unless you enter the correct month then it is void

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

=+impact member states and MNEs?

Answered: 1 week ago