Answer:
The program to this question can be given as:
Program:
//define header file.
#include<iostream>
#include<string>
using namespace std;
class invalidDay //define class.
{
string msg; //define variable as private.
public:
invalidDay() //constructor.
{
msg="Day input is wrong";
}
void showException() //define function.
{
cout<<msg<<endl;
}
};
class invalidMonth //define class.
{
string msg; //define variable as private.
public:
invalidMonth() //constructor.
{
msg="Month input is wrong";
}
void showException() //define function.
{
cout<<msg<<endl;
}
};
class leapYear //define class
{
string msg; //define variable as private.
public:
leapYear() //define constructor
{
msg="year input is wrong";
}
void showException() //define function.
{
cout<<msg<<endl;
}
};
void read_date(int &day,int &month,int &year); //declaration of function.
void read_date(int &d,int &m,int &y) //defination of function.
{
//function body.
cout<<"Enter day"<<endl;
cin>>d;
if(d<=0 ||d>31)
throw invalidDay(); //Exception.
cout<<"Enter month"<<endl;
cin>>m;
if(m<=0 ||m>=13)
throw invalidMonth();
cout<<"Enter year"<<endl;
cin>>y;
if(y%400==0||(y!=100&&y%4==0))
if(d>=30)
throw leapYear();
}
int main() //main function
{
//define variable
int day,month,year;
string months[12]={"January","February","March",
"April","May","June","July","August","September",
"October","November","December"};
//Exception handling.
try //try block
{
read_date(day,month,year);
cout<<"Date of Birth "<<months[month-1]<<" "<<day<<","<<year;
}
//catch bolck.
catch(invalidDay id)
{
id.showException();
}
catch(invalidMonth im)
{
im.showException();
}
catch(leapYear ly)
{
ly.showException();
}
return 0;
}
Output:
Enter day
12
Enter month
12
Enter year
2019
Date of Birth December 12,2019.
Explanation:
In the above program firstly we define 3 class that is invalidDay, invalidMonth, and leapYear. In these classes, we define a method, variable, and constructor, and in the constructor, we holding the value of the msg(variable). Then we define a function that is read_data. This function collects all values from the user and throws into leap year function. In the last, we define the main function this function used exception handling. In the exception handling, we use multiple catch block with try block. After handling all the exception we called all function of the class.