Write a C++ program to check the duplicate mobile number by throwing a custom exception.
The class named ContactDetail with the following private member variables
Data typeVariable namestringmobilestringalternateMobilestringemail
In ContactDetail class include the following member function
Member FunctionDescriptionvoid display() This function is used to display the contact details
Create a class named ContactDetailBO with the following member function
Method nameDescriptionbool validate(ContactDetail cd) This method is used to validate whether the mobile number and alternate mobile number are not the same. If both are the same, then it throws DuplicateMobileNumber Exception. Otherwise, return true to print the details.Class named DuplicateMobileNumberException thatextends exception class and create
a method to return the following message "DuplicateMobileNumberException".
#include <iostream>
class ContactDetail {
private:
std::string mobileNumber;
std::string alterMobileNumber;
std::string email;
public:
ContactDetail() {
}
ContactDetail(std::string mobileNumber, std::string alterMobileNumber, std::string email) {
this->mobileNumber = mobileNumber;
this->alterMobileNumber = alterMobileNumber;
this->email = email;
}
void display() {
std::cout << "mobile number:" << this->mobileNumber << std::endl;
std::cout << "alternative mobile number:" << this->alterMobileNumber << std::endl;
std::cout << "email:" << this->email << std::endl;
}
std::string getMobileNumber() {
return this->mobileNumber;
}
std::string getAlternativeMobileNumber() {
return this->alterMobileNumber;
}
};
class ContactDetailBO : public ContactDetail {
public:
bool validate(ContactDetail contactDetail) {
if (contactDetail.getMobileNumber() == contactDetail.getAlternativeMobileNumber())
throw "DuplicateMobileNumberException";
else return true;
}
};
class DuplicateMobileNumberException : public std::exception {
public:
const char * what () const throw () {
return "DuplicateMobileNumberException";
}
};
int main() {
return 0;
}
Comments
Leave a comment