Write a program to print the following details
• Assign and print the ID of each employee,
• Phone number and Address of two staff members having names "ali" and "asif" respectively by creating two objects of the class 'Employee'.
• throw an exception in case the phone number of employee is inserted in float , double or char value.
• throw an exception in case the ID of an employee is inserted in float , double or char value.
#include <iostream>
#include <string>
using namespace std;
class Employee{
    int id, phone_number;
    string name;
    public:
    Employee(string n, int ID): name(n), id(ID){}
    void getDetails(){
        string s = "\nException caught!", b;
        try{
            cout<<"\nEnter "<<name<<" id: ";
            cin>>b;
            for(int i = 0; i < b.length(); i++)
                if(b[i] > '9' || b[i] < '0') throw(s);
            id = stoi(b);
        }
        catch(string s){
            cout<<s;
        }
        try{
            cout<<"\nEnter "<<name<<" phone number: ";
            cin>>b;
            for(int i = 0; i < b.length(); i++)
                if(b[i] > '9' || b[i] < '0') throw(s);
            phone_number = stoi(b);
        }
        catch(string s){
            cout<<s;
        }
    }
    void printId(){
        cout<<"\nName: "<<name<<"\nID: "<<id<<endl;
    }
};
int main(){
    Employee Ali("Ali", 323134), Asif("Asif", 8467437);
    Ali.printId();
    Asif.printId();
    Ali.getDetails();
    Asif.getDetails();
    return 0;
}
Comments
Leave a comment