C++

Create Date Class in C++

In this article, we’ll look at how to manipulate date classes in the C++ Programming language. The C++ language incorporates date and time methods and classes. To manage time and date, we must integrate the <ctime> library into the programs. The class ‘Date’ contains a default constructor with three arguments that enable the date to be adjusted when a date item is constructed.

Create a Date Class to Acquire the Objects

The class contains a date() function that provides the current calendar date as an instance. Using (->) this pointer to the date variable, we will retrieve a variety of data. This C++ code has been compiled and executed successfully.

#include<iostream>

#include<ctime>

 

std::string months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
std::string days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
                      "Sat"};

class Date{

    private:
std::string month;
std::string day;
        int date;
        int year;

    public:

Date() {
                const int BASE_YEAR = 1900;
time_t timer;
                tm * time;
std::time(&timer);
                time = localtime(&timer);
                date = time->tm_mday;
                month = months[time->tm_mon];
                day = days[time->tm_wday];
                year = time->tm_year + BASE_YEAR;
        }

First of all we include two important header files <iostream> and <ctime>. We declare two arrays and specify the elements of these arrays. The first array represents the names of 12 months, and the second array represents the names of weekdays. These arrays are formed by using the ‘std::string’ method. We declare a class named ‘Date’. Its private components include the array of months, array of days, and variables for storing date and year. These variables have an ‘integer’ data type.

In the next step, we specify the public members of the class. The default constructor ‘Date()’ has been invoked. We set the value of the ‘BASE_YEAR’, which has the ‘int’ data type. We create a pointer ‘tm *’ for the time. We call the function std::time() and pass the ‘timer’ as an argument of this function. The ‘time’ should be pointed to the variable ‘tm_mday’ by using the (->) operator. The ‘time’ will be pointed to the ‘tm_mon’ to indicate the month.

For acquiring the day, ‘tie’ will be pointed to the variable ‘tm_wday’. For obtaining the year, the value of ‘tm_year‘ would be added to the value of ‘BASE_YEAR’.

void printDate(void)
        {
std::cout<< "The current date: "
<month << " " <day << " "
<date  << " " <year;
        }

        ~Date() {}
};

int main()
{
    Date d;

d.printDate();
}

In addition to this, we have called the printDate() function. To show the line ‘The current date:’ just before the date, we utilized the ‘std::cout’. To get the month, day, date, and year ‘this’ pointer is being applied. The destructor ‘~Date’ is invoked. We start the coding in the body of the main() function. We initialize the variable ‘d’ for storing the date. In the end, we call the printDate() method to illustrate the date.

Use Parameterized Constructor

Construct a Date class in a program with three variables for storing the data: dd, mm, and yyyy. With the help of a parameterized constructor, we define and configure the object and represent the date in dd-mon-yyyy notation.

#include<conio.h>

#include<iostream>

using namespace std;

class date
{
    int dd,mm,yyyy;

   public:
date(int d,intm,int y)
   {
    dd=d;
      mm=m;
yyyy=y;
   }
   void disp()
   {
    if(mm>12)
cout<<"Invalid Month.";
      else
        cout<<"Input   : "<<dd<<"/"<<mm<<"/"<<yyyy<<endl;
      if(mm==1)
      {
    cout<<"Output  : "<<dd<<"/"<<"jan"<<"/"<<yyyy;
      }
      else if(mm==2)
      {
    cout<<"Output  : "<<dd<<"/"<<"Feb"<<"/"<<yyyy;
      }

Here we integrate the libraries <conio.h> and <iostream> just before the standard namespace. We have created the class ‘date’. Here we initialize the variable ‘dd’ for the day, ‘mm’ for a month, and ‘yyyy’ for a year. We construct the parameterized constructor of this class and set it publicly. We have called the disp() function.

Within the body of this function, we apply the if-else-if condition. If the user entered the value of a month greater than 12, the ‘cout’ prints the text ‘Invalid Month’ on the screen. Otherwise, ‘cout’ displays the word ‘Input’. If the user enters the value of month 1, ‘cout’ prints the month ‘jan’. ‘cout’ prints the ‘feb’ month if the user provides the value of month 2.

else if(mm==3)
      {
    cout<<"Output  : "<<dd<<"/"<<"mar"<<"/"<<yyyy;
      }
      else if(mm==4)
      {
    cout<<"Output  : "<<dd<<"/"<<"apr"<<"/"<<yyyy;
      }
      else if(mm==5)
      {
    cout<<"Output  : "<<dd<<"/"<<"may"<<"/"<<yyyy;
      }
      else if(mm==6)
      {
    cout<<"Output  : "<<dd<<"/"<<"jun"<<"/"<<yyyy;
      }
      else if(mm==7)
      {
    cout<<"Output  : "<<dd<<"/"<<"july"<<"/"<<yyyy;
      }
      else if(mm==8)
      {
    cout<<"Output  : "<<dd<<"/"<<"Aug"<<"/"<<yyyy;
      }
      else if(mm==9)
      {
    cout<<"Output  : "<<dd<<"/"<<"sep"<<"/"<<yyyy;
      }
      else if(mm==10)

The ‘cout’ prints the ‘mar’ month if the user specifies the value of month 3. ‘cout’ shows the ‘apr’ month if the user enters the value for month 4, and so on.

{
    cout<<"Output  : "<<dd<<"/"<<"oct"<<"/"<<yyyy;
      }
      else if(mm==11)
      {
    cout<<"Output  : "<<dd<<"/"<<"Nov"<<"/"<<yyyy;
      }
      else if(mm==12)
      {
    cout<<"Output  : "<<dd<<"/"<<"Dec"<<"/"<<yyyy;
      }
   }
};
int main()
{
   int d,m,y;
cout<>d;
cout<>m;
cout<>y;
   date d1(d,m,y);
   d1.disp();
getch();
return(0);
}

Furthermore, we have been called the main() function. Here first, we initialize the variable for storing the day, month, and year. The ‘cout’ command is applied to print the line, so the user has entered the numeric form’s day, month, and year values. In the end, we create an illustration of the class ‘date’. We employ disp() to that instance. To hold the output on the screen for a while, we have used getch(), and to terminate the program, we add the ‘return 0’ command.

Utilize set() and get() Function

Define a ‘Date’ class that contains three data items: a month (int), a day (int), and a year (int).  Constructors having different arguments will be included in the class, and the arguments are used to configure these data items. Every data item will have a set() and get() function. Therefore provide a displayDate() function that shows the date.

#include<iostream>

#include<string>

using namespace std;

class Date
{
 public:
Date( int, int, int );
  void setMonth( int );
  int getMonth();
  void setDay(int);
  int getDay();
  void setYear(int);
  int getYear();
  void displayDate();
 private:
  int month;
  int day;
  int year;
};
Date::Date( int m, int d, int y)
{
 if(m>=1&&m<=12)
  month=m;

At the start of the program, we introduced the libraries <iostream> and <string>, and then we used the standard namespace. The class named ‘Date’ has been declared. The public members of this class are defined. Date constructor, void setMonth(), getMonth(), setDay(), getDay(), setYear(), getYear(), and diplayDate() are all public elements . Different functions are represented by these members.

The class’s private elements are specified in the next step. Month, day, and year will be included in it. We have utilized the function Date::Date(). If-else condition is applied. The month’s value must be greater than 1 and less than 12.

else
 {
  month=1;
 }
 day=d;
 year=y;
}
void Date::setMonth( int m)
{
 if(m>=1&&m<=12)
  month=m;
 else
 {
  month=1;
 }
}
void Date::setDay(int d )
{
 day = d;
}
void Date::setYear(int y )
{
 year=y;
}
int Date::getMonth()
{
 return month;
}
int Date::getDay()
{
 return day;
}
int Date::getYear()
{
 return year;
}
void Date::displayDate(){
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
int main()
{
 Date Date1(5,7,2022);
cout<<"Date is ";
 Date1.displayDate();
}

The setMoth() function is being used to adjust the month value. The setDay() function is applied to set the day’s value. Similarly, we used the setYear() function to specify the year value. We have now used the get() function to get the month, day, and year values separately. We have used the displayDate() function to acquire the entire date.

In the end, we have invoked the main() function. We have created a member ‘Date1’ and set the date. The ‘cout’ command prints the date, and also we employ the displayDate() method once again.

Conclusion

We have talked about the details of the date class in this article. We’ve covered three different techniques for creating the ‘Date’ class. These demonstrations of methods are well-explained and successfully implemented.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content