Create Employee class definition represents a commission employee. - C++ Class

C++ examples for Class:Member Function

Description

Create Employee class definition represents a commission employee.

Demo Code

                                                                                                                                                                  
#include <string>
#include <iostream> 
using namespace std; 
                                                                                                                                                                  
class Employee //from   ww w.j  av  a 2  s. c  o m
{ 
public: 
   Employee( const string &, const string &, const string &, double = 0.0, double = 0.0 ); 
                                                                                                                                                                  
   void setFirstName( const string & ) ; // set first name 
   string getFirstName() const; // return first name 
                                                                                                                                                                  
   void setLastName( const string & ) ; // set last name 
   string getLastName() const; // return last name 
                                                                                                                                                                  
   void setSocialSecurityNumber( const string & ) ; // set SSN 
   string getSocialSecurityNumber() const; // return SSN 
                                                                                                                                                                  
   void setGrossSales( double ); // set gross sales amount 
   double getGrossSales() const; // return gross sales amount 
                                                                                                                                                                  
   void setCommissionRate( double ); // set commission rate (percentage) 
   double getCommissionRate() const; // return commission rate 
                                                                                                                                                                  
   double earnings() const; // calculate earnings 
   void print() const; // print Employee object 
private: 
   string firstName; 
   string lastName; 
   string socialSecurityNumber; 
   double grossSales; // gross weekly sales 
   double commissionRate; // commission percentage 
}; // end class Employee 
                                                                                                                                                                  
                                                                                                                                                                  
Employee::Employee( const string &first, const string &last, const string &ssn , double sales, double rate ) 
{ 
   firstName = first; // should validate 
   lastName = last; // should validate 
    socialSecurityNumber = ssn; // should validate 
    setGrossSales( sales ); // validate and store gross sales 
    setCommissionRate( rate ); // validate and store commission rate 
}
                                                                                                                                                                  
void Employee::setFirstName( const string &first ) 
{ 
   firstName = first; // should validate 
}
                                                                                                                                                                  
string Employee::getFirstName() const 
{ 
    return firstName; 
}
                                                                                                                                                                  
void Employee::setLastName( const string &last ) 
{ 
   lastName = last; // should validate 
}
                                                                                                                                                                  
string Employee::getLastName() const 
{ 
    return lastName; 
}
                                                                                                                                                                  
void Employee::setSocialSecurityNumber( const string &ssn ) 
{ 
    socialSecurityNumber = ssn; // should validate 
}
                                                                                                                                                                  
string Employee::getSocialSecurityNumber() const 
{ 
    return socialSecurityNumber; 
}
                                                                                                                                                                  
void Employee::setGrossSales( double sales ) 
{ 
    if ( sales >= 0.0 ) 
       grossSales = sales; 
   else 
       throw invalid_argument( "Gross sales must be >= 0.0" ); 
}
                                                                                                                                                                  
double Employee::getGrossSales() const 
{ 
    return grossSales; 
} // end function getGrossSales 
                                                                                                                                                                  
void Employee::setCommissionRate( double rate ) 
{ 
    if ( rate > 0.0 && rate < 1.0 ) 
       commissionRate = rate; 
   else 
       throw invalid_argument( "Commission rate must be > 0.0 and < 1.0" ); 
}
                                                                                                                                                                  
// return commission rate 
double Employee::getCommissionRate() const 
{ 
    return commissionRate; 
}
                                                                                                                                                                  
// calculate earnings 
double Employee::earnings() const 
{ 
    return commissionRate * grossSales; 
}
                                                                                                                                                                  
// print Employee object 
void Employee::print() const 
{ 
    cout << "commission employee: " << firstName << ' ' << lastName 
       << "\nsocial security number: " << socialSecurityNumber 
       << "\ngross sales: " << grossSales 
       << "\ncommission rate: " << commissionRate; 
}
                                                                                                                                                                  
#include <iostream> 
#include <iomanip> 
using namespace std; 
                                                                                                                                                                  
int main() 
{ 
    Employee employee( "S", "S", "222-22-2222", 10000, .06 ); 
                                                                                                                                                                  
    cout << fixed << setprecision( 2 ); 
                                                                                                                                                                  
    cout << "Employee information obtained by get functions: \n" 
       << "\nFirst name is " << employee.getFirstName() 
       << "\nLast name is " << employee.getLastName() 
       << "\nSocial security number is " 
       << employee.getSocialSecurityNumber() 
       << "\nGross sales is " << employee.getGrossSales() 
       << "\nCommission rate is " << employee.getCommissionRate() << endl; 
                                                                                                                                                                  
   employee.setGrossSales( 8000 ); // set gross sales 
   employee.setCommissionRate( .1 ); // set commission rate 
                                                                                                                                                                  
   cout << "\nUpdated employee information output by print function: \n" 
       << endl; 
   employee.print(); // display the new employee information 
                                                                                                                                                                  
   // display the employee's earnings 
   cout << "\n\nEmployee's earnings: $" << employee.earnings() << endl; 
}

Result


Related Tutorials