133k views
3 votes
Write a program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)

1 Answer

1 vote

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.

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.

User Hrr
by
5.7k points