473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Inheritance of Classes

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

using namespace std;

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

void displayApplication()
{
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 terminateApplication()
{
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(string first);
string getLastName();
void setLastName(string last);
char getGender();
void setGender(char gen);
int getDependents();
void setDependents(int dep);
void setDependents(string dep);
static int getNumEmployees();
static int numEmployees;
class Salaried;
class Hourly;
};

Employee::Employee()
{
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(payType));

if (payType = 'Y')
{
Employee::Salaried;

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

setFirstName(firName);
setLastName(lasName);
setGender(toupper(gen));
setDependents(dep);
}

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

string Employee::getFirstName()
{
return firstName;
}

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

string Employee::getLastName()
{
return lastName;
}

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

char Employee::getGender()
{
return gender;
}

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

int Employee::getDependents()
{
return dependents;
}

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

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

int Employee::getNumEmployees()
{
return numEmployees;
}

void Employee::displayE()
{
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 getManagementLevel();
void setManagementLevel(int mLevel);
void displaySalaried();
};

Salaried::Salaried()
{
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);
setManagementLevel(manageLevel);
}

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

double Salaried::getAnnualSalary()
{
return annualsalary;
}

void Salaried::setAnnualSalary(double salary)
{
annualsalary = salary;
}

int Salaried::getManagementLevel()
{
return managementLevel;
}

void Salaried::setManagementLevel(int mLevel)
{
managementLevel = mLevel;
}

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

return weeklyPay;
}

void Salaried::displaySalaried()
{
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(string 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(empHours);
setCategory(empCategory);
}

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::getHours()
{
return hours;
}

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

string Hourly::getCategory()
{
return category;
}

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

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

int Employee::numEmployees = 0;

int main()
{
int numEmp;

displayApplication();

displayDivider("Employee 1");

Employee practiceOne;
//Salaried practiceSalary;
practiceOne.displayE();
//practiceOne.Benefits.displayBenefits();
//practiceSalary.displaySalaried();


Employee::numEmployees++;

numEmp = Employee::getNumEmployees();
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.
Feb 11 '12 #1

✓ answered by weaknessforcats

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
Expand|Select|Wrap|Line Numbers
  1. Employee* emp;
  2. //etc...
  3.           emp = new Hourly;
  4. //or:
  5.  
  6.           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::MethodX() 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.

4 3330
I have a lot of problems with my code I just want to know how to solve that particular problem at the moment.
Feb 11 '12 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Feb 11 '12 #3
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?
Feb 11 '12 #4
weaknessforcats
9,208 Expert Mod 8TB
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
Expand|Select|Wrap|Line Numbers
  1. Employee* emp;
  2. //etc...
  3.           emp = new Hourly;
  4. //or:
  5.  
  6.           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::MethodX() 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.
Feb 12 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

145
by: David MacQuigg | last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating classes in Python. I'm trying to figure out what fundamental benefit there is to having classes. Is all this complexity...
3
by: Allen | last post by:
Hi all, I'm still having a hard time with my derived class. I know I make reference to some MS-specific tech. but the question should be independent of that; I'm having a hard time with the...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
1
by: Simon Harris | last post by:
Hi All, I'm new to asp.net (Migrating from 'Classic' ASP) I'm having troubles working out classes, functions etc... Current situation is this: index.aspx displays datalist with links to...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
1
by: nickyeng | last post by:
supposed i have Health class and Object class. Inherit directly from Object class, would be something like this: class Health : public Object { /** **/ }; What if indirectly way?...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
11
by: George Sakkis | last post by:
I have a situation where one class can be customized with several orthogonal options. Currently this is implemented with (multiple) inheritance but this leads to combinatorial explosion of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.