Write a C++ program to create a class Date which contains three data members as dd, mm, yyyy. Create and initialize the object by using parameterized constructor and display date in dd-mon-yyyy format. (Input: 19-12-2014 Output: 19-Dec-2014) Perform validation for month.

//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;

#include<conio.h>
#include<iostream.h>
class date
{
int dd,mm,yyyy;

   public:
   date(int d,int m,int y) // creating parameterized constructor
   {
    dd=d;
      mm=m;
      yyyy=y;
   }
   void disp()
   {
    if(mm>12)
                cout<<"Invalid Month."; //mm is month if it is greater than 12 then it is invalid.
      else
cout<<"Input   : "<<dd<<"/"<<mm<<"/"<<yyyy<<endl;
      if(mm==1)
      {
      cout<<"Output  : "<<dd<<"/"<<"jan"<<"/"<<yyyy;
      }
      else if(mm==2)
      {
      cout<<"Output  : "<<dd<<"/"<<"Feb"<<"/"<<yyyy;
      }
      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)
      {
      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<<"Enter a date : ";
   cin>>d;
   cout<<"Enter a month: ";
   cin>>m;
   cout<<"Enter a year : ";
   cin>>y;
   date d1(d,m,y); //creating instance of class date
   d1.disp();
   getch();
   return(0);
}

0 Comments