Write a C++ program to check for the not-eligible donor by throwing a custom exception.
The class Donor has the following protected variables.
Data TypeVariablestringnameintageintweightstringbloodGroup
In the Donor class, define the following function.
MethodDescriptionbool validateDonor(Donor donor)This method validates donor eligibility. Validate the donor details with the following conditions.
1.Age must be greater than 30 then the donor is eligible.
2.If the age is less than 30 then throw an exception and check the weight should be greater than 44 kg in catch block,
If the weight is also less than 44 then the donor is not eligible and rethrow the exception to the main method.
If the weight is greater than 44 then the donor is eligible.
In the Main method, read the donor details from the user and create corresponding objects.
Validate the donor to check the donor is eligible or not.
#include <iostream>
#include <string>
#include <exception>
using namespace std;
class Donor {
public:
Donor(string name, int age, int weight, string bloodGroup) :
name(name), age(age), weight(weight), bloodGroup(bloodGroup)
{}
string getName() const { return name; }
int getAge() const { return age; }
int getWeight() const { return weight; }
string getBloodGroup() const { return bloodGroup; }
bool validateDonor(Donor donor);
private:
string name;
int age;
int weight;
string bloodGroup;
};
bool Donor::validateDonor(Donor donor) {
bool eligible = false;
try {
if (donor.age > 30) {
eligible = true;
}
if (donor.age < 30) {
throw exception();
}
}
catch (...) {
if (donor.weight < 44) {
throw exception();
}
if (donor.weight > 44) {
eligible = true;
}
}
return eligible;
}
int main() {
string name;
cout << "Enter donor's name: ";
cin >> name;
int age;
cout << "Enter donor's age: ";
cin >> age;
int weight;
cout << "Enter donor's weight: ";
cin >> weight;
string blGroup;
cout << "Enteh donor's blood group: ";
cin >> blGroup;
Donor donor(name, age, weight, blGroup);
try {
cout << "Donor " << donor.getName();
if (donor.validateDonor(donor)) {
cout << " is eligible" << endl;
}
else {
cout << " is not eligible" << endl;
}
}
catch (...) {
cout << " don't pass validation" << endl;
}
return 0;
}
Comments
Leave a comment