Question

C++ PROGRAMMING: Define a class called Date that has 3 private members that represent the month,...

C++ PROGRAMMING: Define a class called Date that has 3 private members that represent the month, day, and year respectively. They will be represented as integers. Include a constructor and a public void function called print that prints out the date. (For example, if the month is 2, the day is 25, and the year is 1946, print provides as output 2/25/1946.) Include the class in a program that prints out the date 2/25/1946. Also, create another function that will translate the month number and output the date in the following format as well (using this example): “The date entered is February 25, 1946.”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>
#include <sstream>
using namespace std;
class Date
{
private:
// Declaring instance variables
int month;
int day;
int year;
public:
// Zero argumented constructor
Date()
{
}
// Parameterized constructor
Date(int d, int m, int y)
{
day = d;
month = m;
year = y;
}
string getMonthInString() const
{
// Declaring char array
string months[] = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };
return months[month - 1];
}
string displayDateDefaultForm()
{
stringstream ss;
ss << month << "/" << day << "/" << year << endl;
return ss.str();
}
string displayDateMonName()
{
stringstream ss;
ss << getMonthInString() << " " << day << ", " << year << endl;
return ss.str();
}
string displayMonthInMiddle()
{
stringstream ss;
ss << day << " " << getMonthInString() << " " << year << endl;
return ss.str();
}

void setDay(int d)
{
this->day = d;
}
int getDay(int d)
{
return d;
}
void setMonth(int m)
{
this->month = m;
}
int getMonth()
{
return month;
}
void setYear(int y)
{
this->year = y;
}
int getYear()
{
return year;
}
};
int main()
{
// Declaring variables
int month, day, year;
while (true)
{
// Getting the input entered by the user
cout << "Enter Day:";
cin >> day;
if (day < 1 || day > 30)
{
cout << "Invalid.Must be between 1-30" << endl;
continue;
}
else
break;
}
while (true)
{
cout << "Enter Month:";
cin >> month;
if (month < 1 || month > 12)
{
cout << "** Invalid.Must be between 1-12 **" << endl;
continue;
}
else
break;
}
while (true)
{
cout << "Enter Year:";
cin >> year;
if (year < 1900 || year > 2025)
{
cout << "** Invalid.Must be between 1900-2025**" << endl;
continue;
}
else
break;
}

// Creating an instance of Date class
Date d(day, month, year);
cout << endl;
  
cout <<"The date entered is "<< d.displayDateMonName();
  
  
return 0;
}
_________________________

Output:


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
C++ PROGRAMMING: Define a class called Date that has 3 private members that represent the month,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • Create a class called Date212 to represent a date. It will store the year, month and...

    Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String. The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that...

  • using C programming Date Write a program that reads a month and a day in that...

    using C programming Date Write a program that reads a month and a day in that month and prints the date in format "month day". The input is two integers, one between 1 and 12 (inclusive) and one between 1 and 31 (inclusive) and the output is a single line in format "month day". For example, for input 2 10 the program will print "February 10th". The day will always have the ordinal indicator, e.g. 1st, 2nd, 3rd, 4th. Example...

  • Examine the following class definition: public class Date private int year; private int month; private int...

    Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...

  • 15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor init...

    15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor initializes the data members to 0. You need to use the operator?: to test whether x and y are positive. If not, assign them to 0. The function printPoint prints the message "(X, Y)" where X and Y are two integers. In the main program, first input the number...

  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 -...

    Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 - 28. 29. 30. 31 depending on month & year int year;//4-digit, e.g.. 2017 public: date();//Default constructor (investigate; find what it is used for)//Postcondition: the newly declared date object is initialized to 01/01/2000 date(int mm, int dd, int yyyy);//Second constructor//Postcondition: the newly declared data object is initialized to mm/dd/yyyy void setDate(int mm. int dd. int yyyy);//Postcondition: set the contents of the calling date object to...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Create a class named Date in C++ Class has 3 private data items (name the data...

    Create a class named Date in C++ Class has 3 private data items (name the data items month, day, year) int month int day int year Member functions ‘getters’   a getter for each data item Use ‘get’ and the data items name The data item name must be capitalized Return the data item Example: int getMonth()        {return month;} ‘setters’   a setter for each data item Use ‘set’ and the data items name The data item name must be capitalized Change...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT