Monday 29 August 2011

You are required to write a program which will calculate the salary of an employee according to his/her grade assigned by the employer. You must declare a class named CalSalary whose private data members will be employee ID, employee name and employee grade. Also write a constructor, setter and getter functions for all private data members and a function that will calculate the salary.


Detailed Description:

  • Employee ID and grade should be of type integer. Employee name should be of type string.

  • Constructor should initialize employee ID and grade with value zero and employee name with value NULL.

  • For assigning or extracting values from private data members, you must use getter and setter functions.

  • Declare a public member function named calculate which will calculate the net salary of the employee according to his/her grade.

  • Formula for calculating actual salary is (Basic Salary) + 45% of basic salary.

  • If the grade of employee is 17, then the basic salary is 15,000.

  • If the grade is 18, then the basic salary is 20,000 and if grade is 19 then the basic salary is 25,000.




Sample Output 1



Please enter employee ID : 001

Please enter employee name : Mohammad Ali

Please enter employee grade : 17



The net salary of Mohammad Ali is 21750







Sample Output 2



Please enter employee ID : 2

Please enter employee name : Aslam Khan

Please enter employee grade : 19



The net salary of Aslam Khan is 36250



Answer

#include <iostream.h>   
#include <cstring>
#include <conio.h>
//Class will calculate salary of an employee according to his grade
class CalSalary{ 
    private:
        char name[30];          // character string for storing employee name
        int empID;              // employee's ID
        int grade;              // empoyee's grade 
   
    public:
        CalSalary();                // default constructor
       
        //getter methods for all private data members of class
        int getID();
        int getGrade();
        char* getName();
       
        //setter methods for all private data members of class
        void setName(char[]);
        void setID(int);
        void setGrade(int);
       
        //This function will calculate salary of employee on basis of his grade
        void calculate();
};

  //Definition of default constructor which initializes employee name with Null and id and grade with zero
  CalSalary::CalSalary()
      {
          strcpy(name, "");
          empID = 0;
          grade = 0;
         
      }   

   int CalSalary::getID()  // Getter method for private member ID
      {
          return empID;    // returning employee's ID
      
      }
   int CalSalary::getGrade()  // getter mehtod for private member Grade
      {
          return grade;     // returning employee's grade
      
      }

    char* CalSalary::getName()  // getter mehtod for private member Name
      {
          return name;     // returning employee's name
      
      }
   void CalSalary::setID(int id)   // setter method for private member ID
      {
          empID = id;      // Setting id equal to the parameter given to setID()
      
      }

   void CalSalary::setGrade(int GRADE)    // Setter method for grade
      {
          grade = GRADE;       // Setting grade equal to the parameter given to setGrade()
      
      }
   void CalSalary::setName(char NAME[])   // Setter method for private member name, taking a character array as argument
      {
          strcpy (name, NAME);    // strcpy function is used to copy the character array in parameter to the private member name
      
      }
  
 
 
   void CalSalary::calculate()  // This function will calculate employee's salary
      {
          int basic = 0;   // This variable represents basic salary of employee
         
          if(grade == 17)  // If employee's grade is 17 then setting basic salary = 15000
            {
              basic = 15000;
            }       
          if(grade == 18)  // If employee's grade is 18 then setting basic salary = 20000
            {
              basic = 20000;
            }       
          if(grade == 19)  // If employee's grade is 19 then setting basic salary = 25000
            {
              basic = 25000;
            }       
      
     
      // Salary is calculated as basic salary + ( 45% of basic salary)
      cout << endl << "Net salary of " << getName() << " is Rs." << (basic + (basic * 45 / 100));
    }


int main()
 {
   CalSalary obj1;  // creating an object of class CalSalary
   int Grade, id;
   char Name[30];       
    // Taking input from user for employee ID
    cout << "Please enter employee ID : ";
    cin >> id;
   
    cin.ignore();
    /* cin.ignor function is used here because when user enters value of ID and presses enter,
     the following statements like gets(Name) will consider the Enter character as input for character string Name
      and will by-pass these lines of code. cin.ignor() function will ignor the enter character so that
       user may enter his/her desired value of employee's name */

    cout << "Please enter employee name : ";  //Taking input from user for employee's Name
    gets(Name);
 
    cout << "Please enter employee grade : "; // Taking input from user for employee's Grade
    cin >> Grade;
 
       while(Grade<17 || Grade>19)   //Here we are putting a check on user's input for grade, which must be 17 - 19
         {
          cout << "Please enter employee grade again (between 17 - 19) : ";
          cin >> Grade;
         }
 
// Using setter functions for setting the employee's name, Id and grade equal to user's input
   obj1.setName(Name); 
   obj1.setID(id);
   obj1.setGrade(Grade);
 
   obj1.calculate();  // calculating salary of employee and displaying on console
 
   getch();
 } 







No comments:

Post a Comment