Develop in C++ a class date to represent a calendar. The class should provide th
ID: 3792257 • Letter: D
Question
Develop in C++ a class date to represent a calendar. The class should provide the following operations:
• A default constructor that initializes a date object to 01-01-1900.
• A class constructor that initializes a date object to a correct value using three integer parameters corresponding to the desired month, day and year.
• The function toString() that returns the string version of a date object. For example, applying toString() to the date 12-01-2000 produces "December 1st, 2000".
• The function nextDate() that returns the successive date i.e. the new value of the date object. For example, applying nextDate() to the date 12-31-2000 produces a new date: 01-01-2001. You should take into account if the year is a leap year or not. A leap year is: (1) divisible by 400 or (2) divisible by 4 and not divisible by 100.
• The function compareDates() that checks if the date of interest is before, after or equal to the argument date.
A simple run of the driver program follows.
Enter the first date using the format mm-dd-yyyy: 12-32-2000 Incorrect day!
Enter the first date using the format mm-dd-yyyy: 12-31-2000
The string version of the date is: December 31st, 2000 The next date in string version is: January 1st, 2001
Enter the second date using the format mm-dd-yyyy: 12-01-2001
The first date comes before the second one.
Another run: Enter the first date using the format mm-dd-yyyy: 02-28-2005
The string version of the date is: February 28th, 2005
The next date in string version is: March 1st, 2005
Enter the second date using the format mm-dd-yyyy: 01-10-2005
The first date comes after the second one.
Hand In
1. The header, implementation and driver files should be respectively named : Calendar.h, Calendar.cpp and TestCalendar.cpp
Explanation / Answer
#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char *s;
int size;
public:
String(const char *str = NULL); // constructor
~String() { delete [] s; }// destructor
String(const String&); // copy constructor
void print() { cout << s << endl; } // Function to print string
void change(const char *); // Function to change
};
String::String(const char *str)
{
size = strlen(str);
s = new char[size+1];
strcpy(s, str);
}
void String::change(const char *str)
{
delete [] s;
size = strlen(str);
s = new char[size+1];
strcpy(s, str);
}
String::String(const String& old_str)
{
size = old_str.size;
s = new char[size+1];
strcpy(s, old_str.s);
}
int main()
{
String str1("GeeksQuiz");
String str2 = str1;
str1.print(); // what is printed ?
str2.print();
str2.change("GeeksforGeeks");
str1.print(); // what is printed now ?
str2.print();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.