Answer to Question #242996 in C++ for vishal

Question #242996

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.



 


1
Expert's answer
2021-09-27T10:16:47-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS