Answer to Question #213933 in C++ for Maryam

Question #213933

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.


1
Expert's answer
2021-07-05T13:29:03-0400
#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;
}

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