11- Write a class called Employee that has two integer member variables variable
ID: 3705956 • Letter: 1
Question
11- Write a class called Employee that has two integer member variables variable called salary. The Employee must have setter and getter range 0 to 100 then an exception must be setld method exception must be thrown by setAge method. tries to should be able to distinguish between different e Employee must overload stream extraction valid, and, if not, it should set the failbit called id and age and a double member for each member variable. If the id is not in the own by setid method. Also if the age is not in the range 20 to 80 an If main tries to setid or setAge and an exception was thrown, main the two different exceptions and display the appropriate message operator function. It should determine whether the data entered is to indicate improper input. The input must be of the form two integers (id input is: 12,45 234.54. The first the main should keep reading the values until correct format is e onowed by a space and the double value which is salary. For example, a valid value is id, the second is age and third value is salary. If incorrect value is entered ntered. Main must test all functions.Explanation / Answer
//Employee.h
#pragma once
#include <iostream>
#include <exception>
using namespace std;
class InvalidIdException : public exception {
};
class InvalidAgeException : public exception {
};
class Employee
{
public:
Employee();
~Employee();
void setId(int value);
void setAge(int value);
void setSalary(double value);
int getId();
int getAge();
double getSalary();
friend std::istream & operator >> (std::istream&, Employee &);
private:
int id;
int age;
double salary;
};
//Employee.cpp
#include "stdafx.h"
#include "Employee.h"
#include <string>
Employee::Employee()
{
}
Employee::~Employee()
{
}
void Employee::setId(int value)
{
if (value < 0 || value >100)
{
throw InvalidAgeException();
}
id = value;
}
void Employee::setAge(int value)
{
if (value < 20 || value >80)
{
throw InvalidAgeException();
}
age = value;
}
void Employee::setSalary(double value)
{
salary = value;
}
int Employee::getId()
{
return id;
}
int Employee::getAge()
{
return age;
}
double Employee::getSalary()
{
return salary;
}
istream& operator >> (istream& is, Employee& employee)
{
string ip;
getline(is, ip);
int ind = ip.find(" ");
if (ind == string::npos)
{
cout << "WRONG INPUT FORMAT";
return is;
}
string id_age = ip.substr(0, ind);
string nsal = ip.substr(ind + 1);
int comma_ind = id_age.find(",");
if (comma_ind == string::npos) {
cout << "WRONG INPUT FORMAT";
}
string nid = id_age.substr(0, comma_ind);
string nage = id_age.substr(comma_ind + 1);
employee.setId(atoi(nid.c_str()));
employee.setAge(atoi(nage.c_str()));
employee.setSalary(atof(nsal.c_str()));
return is;
}
//Main.cpp
#include "Employee.h"
#include <iostream>
#include <exception>
int main()
{
try
{
Employee employee;
std::cin >> employee;
std::cout << employee.getId() << " " << employee.getAge() << " " << employee.getSalary();
}
catch (InvalidAgeException)
{
std::cout << "Invalid age";
}
catch (InvalidIdException)
{
std::cout << "Invalid id";
}
catch (exception)
{
std::cout << "Other exceptions";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.