Inheritance of Classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CALIxPAPI916
    New Member
    • Feb 2012
    • 5

    #1

    Inheritance of Classes

    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <iomanip>

    using namespace std;

    int MAX_dependents = 10;
    char default_gender = 'U';

    void displayApplicat ion()
    {
    cout << "Welcome to your first Object Oriented Program--Employee Class \nCIS247C, Week 2 Lab \nName: Anthony Rodriguez";
    }

    void displayDivider( string message)
    {
    cout << "\n************ **** " + message + "************** **\n";
    }

    void terminateApplic ation()
    {
    cout << "\n\nThe end of the CIS247 Week1 iLab. \n";
    }


    class Employee
    {
    private:
    string firstName;
    string lastName;
    char gender;
    int dependents;
    public:
    Employee();
    Employee(string first, string last, char gen, int dep);
    void displayE();
    string getFirstName();
    void setFirstName(st ring first);
    string getLastName();
    void setLastName(str ing last);
    char getGender();
    void setGender(char gen);
    int getDependents() ;
    void setDependents(i nt dep);
    void setDependents(s tring dep);
    static int getNumEmployees ();
    static int numEmployees;
    class Salaried;
    class Hourly;
    };

    Employee::Emplo yee()
    {
    firstName = "N/A";
    lastName = "N/A";
    gender = 'U';
    dependents = 0;
    numEmployees = 0;

    string firName;
    string lasName;
    char gen;
    int dep;
    char payType;

    cout << "Please enter employee's first name: ";
    cin >> firName;
    cout << "Please enter employee's last name: ";
    cin >> lasName;
    cout << "Please enter employee's gender: ";
    cin >> gen;
    cout << "Please enter employee's number of dependents: ";
    cin >> dep;
    cout << "Please enter employee's pay category (Y = Yearly or H = Hourly): ";
    cin >> payType;
    payType = (toupper(payTyp e));

    if (payType = 'Y')
    {
    Employee::Salar ied;

    }
    if (payType = 'H')
    {
    Employee::Hourl y;
    }

    setFirstName(fi rName);
    setLastName(las Name);
    setGender(toupp er(gen));
    setDependents(d ep);
    }

    Employee::Emplo yee(string first, string last, char gen, int dep)
    {
    firstName = first;
    lastName = last;
    gender = gen;
    dependents = dep;
    }

    string Employee::getFi rstName()
    {
    return firstName;
    }

    void Employee::setFi rstName(string first)
    {
    firstName = first;
    }

    string Employee::getLa stName()
    {
    return lastName;
    }

    void Employee::setLa stName(string last)
    {
    lastName = last;
    }

    char Employee::getGe nder()
    {
    return gender;
    }

    void Employee::setGe nder(char gen)
    {
    gender = gen;
    if ((gender == 'M') || (gender == 'F'))
    {
    gender = gen;
    }
    else
    {
    gender = default_gender;
    }
    }

    int Employee::getDe pendents()
    {
    return dependents;
    }

    void Employee::setDe pendents(int dep)
    {
    dependents = dep;
    if (dependents > MAX_dependents)
    {
    dependents = MAX_dependents;
    }
    }

    void Employee::setDe pendents(string dep)
    {
    dependents = atoi(dep.c_str( ));
    }

    int Employee::getNu mEmployees()
    {
    return numEmployees;
    }

    void Employee::displ ayE()
    {
    cout << endl;
    cout << "Employee Information\n" << endl;
    cout << fixed << setprecision(2) ;
    cout << "First Name: " << firstName << endl;
    cout << "Last Name: " << lastName << endl;
    cout << "Gender: " << gender << endl;
    cout << "Dependents : " << dependents << endl;
    cout << endl;
    }

    class Salaried : public Employee
    {
    private:
    int min_management;
    int max_management;
    double bonus_percent;
    int managementLevel ;
    double annualsalary;
    public:
    Salaried();
    Salaried(double salary, int mLevel);
    double getAnnualSalary ();
    void setAnnualSalary (double salary);
    double calculateP();
    int getManagementLe vel();
    void setManagementLe vel(int mLevel);
    void displaySalaried ();
    };

    Salaried::Salar ied()
    {
    annualsalary = 20000;
    managementLevel = 0;

    int sal;
    int manageLevel;

    cout << "Please enter employee's annual salary: ";
    cin >> sal;
    cout << "Please enter employee's management level: ";
    cin >> manageLevel;

    setAnnualSalary (sal);
    setManagementLe vel(manageLevel );
    }

    Salaried::Salar ied(double salary, int mLevel)
    {
    annualsalary = salary;
    managementLevel = mLevel;
    }

    double Salaried::getAn nualSalary()
    {
    return annualsalary;
    }

    void Salaried::setAn nualSalary(doub le salary)
    {
    annualsalary = salary;
    }

    int Salaried::getMa nagementLevel()
    {
    return managementLevel ;
    }

    void Salaried::setMa nagementLevel(i nt mLevel)
    {
    managementLevel = mLevel;
    }

    double Salaried::calcu lateP()
    {
    double weeklyPay = 0.0;
    weeklyPay = (annualsalary / 52);

    return weeklyPay;
    }

    void Salaried::displ aySalaried()
    {
    cout << "Annual Salary: " << annualsalary << endl;
    cout << "Weekly Pay: " << calculateP() << endl;
    cout << "Management Level: " << managementLevel << endl;
    }

    class Hourly : public Employee
    {
    private:
    double min_wage;
    double max_wage;
    double min_hours;
    double max_hours;
    double wage;
    double hours;
    string category;
    public:
    Hourly();
    Hourly(double eWage, double eHours, string eCategory);
    double getWage();
    void setWage(double eWage);
    double getHours();
    void setHours(double eHours);
    string getCategory();
    void setCategory(str ing eCategory);
    void displayHourly() ;
    };

    Hourly::Hourly( )
    {
    wage = 0;
    hours = 0;
    category = "";

    double empWage;
    double empHours;
    string empCategory;

    cout << "Please enter employee's hourly wage: ";
    cin >> empWage;
    cout << "Please enter employee's hours working: ";
    cin >> empHours;
    cout << "Please enter employee's work category (Full, Part, or Temporary): ";
    cin >> empCategory;

    setWage(empWage );
    setHours(empHou rs);
    setCategory(emp Category);
    }

    Hourly::Hourly( double eWage, double eHours, string eCategory)
    {
    wage = eWage;
    hours = eHours;
    category = eCategory;
    }

    double Hourly::getWage ()
    {
    return wage;
    }

    void Hourly::setWage (double eWage)
    {
    wage = eWage;
    }

    double Hourly::getHour s()
    {
    return hours;
    }

    void Hourly::setHour s(double eHours)
    {
    hours = eHours;
    }

    string Hourly::getCate gory()
    {
    return category;
    }

    void Hourly::setCate gory(string eCategory)
    {
    category = eCategory;
    }

    void Hourly::display Hourly()
    {
    cout << "Wage: " << wage << endl;
    cout << "Hours: " << hours << endl;
    cout << "Category: " << category << endl;
    }

    int Employee::numEm ployees = 0;

    int main()
    {
    int numEmp;

    displayApplicat ion();

    displayDivider( "Employee 1");

    Employee practiceOne;
    //Salaried practiceSalary;
    practiceOne.dis playE();
    //practiceOne.Ben efits.displayBe nefits();
    //practiceSalary. displaySalaried ();


    Employee::numEm ployees++;

    numEmp = Employee::getNu mEmployees();
    cout << "--- Number of Employee Object Created ----" << endl;
    cout << "Number of employees: " << numEmp << endl;
    }

    So my question or problem is that I want to give the user the choice between Yearly or Hourly worker which will make the program choose either Yearly class or Hourly class. I am not entirely sure how to do this. Please someone help me.
  • CALIxPAPI916
    New Member
    • Feb 2012
    • 5

    #2
    I have a lot of problems with my code I just want to know how to solve that particular problem at the moment.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This does not look like an inheritance situation. Your class Employee should have a data member (maybe an enum) with values of HOURLY or YEARLY.

      Personally, I would just have a data member for hourly rate. Then when I needed the annual amount I could just calculate it by multiplying by 2016 (12 months*21 workdays per month *8 hours per day).

      Inheritance is used to isolate common attributes so you don't need to re-define them in the base class.

      So maybe a Contractor is a kind-of Employee because the Contract has a Contract (another class) where and Employee does not. Having Contractor derive from Employee allows your Contractor object to inherit all of the Employee member functions plus be able to use the Contract part of the Contractor object.

      Comment

      • CALIxPAPI916
        New Member
        • Feb 2012
        • 5

        #4
        That makes perfect sense and thank you. But in my class we have to use inheritance but the only other classes in the program we were told to make were the benefits, salaried, and hourly. I know we were supposed to pass things from the employee class to the benefits class but I still did not do that because I was confused. Is there any way I can do what I have posted, with the whole the choice calls the class?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          You might be able to but you would have to use virtual functions in your base class. That would mean you derved classes could not have methods nt in the base class. If they do, then you would need to write a visitor class to access the additional methods in the derived class.

          From what I see up to this point, using virtual functions and visitors would be a stretch.

          You see the problem is that you won't know which class to create until run time. So if HOURLY is chosen you create an Hourly object. Or a Salaried object
          Code:
          Employee* emp;
          //etc...
                    emp = new Hourly;
          //or:
          
                    emp = new Salaried
          ;

          So a derived object is created and assigned to a base class pointer. Now this means only Employee class methods can be called since emp is an Employee address.

          Now you need a little design. If you want to call a method using emp, say MethodX() you need to a) declare MethodX() as a virtual metod in Employee. b) write MethodX() for the Salaried class, c) write MethodX fr the Hourly class.

          If you get this far when you call emp->MethodX(), the actual function called is either Salaried::Metho dX() or Hourly::MethodX () depending upon what kind of object emp is really pointing at. That is why MethodX() is virtual in the base class.

          Do a little reading on polymorphism and you find examples on this.

          Post your code again if you still have questions.

          Comment

          Working...