473,396 Members | 1,725 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,396 software developers and data experts.

Why does my benefit class run in my program without me calling it.

The problem that I am having is when I run my program my Benefit class starts running and I have not called it my main but for some reason it is running first instead of my Employee class. This whole program works fine except for the benefit class starts before my Employee class. Can you help me figure it out.



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. class benefit
  9. {
  10. private:
  11. string healthInsurance;
  12. double lifeInsurance;
  13. int vacationDays;
  14. string PPO;
  15. string HMO;
  16. public:
  17. benefit();
  18. benefit(string health, double life, int vacation);
  19. void displayBenefit();
  20. string getHealthInsurance();
  21. void setHealthInsurance(string healthIns);
  22. double getLifeInsurance();
  23. void setLifeInsurance(double lifeIns);
  24. int getVacationDays();
  25. void setVacationDays(int vacation);
  26. };
  27. class Employee : public iEmployee
  28. {
  29. private:
  30. string firstName;
  31. string lastName;
  32. char gender;
  33. int dependents;
  34. double annualSalary;
  35. benefit Benefit;
  36. public:
  37. Employee();
  38. Employee(string first, string last, char gen, int dep, double salary);
  39. double calculatePay();
  40. void displayEmployee();
  41. string getFirstName();
  42. void setFirstName(string first);
  43. string getLastName();
  44. void setLastName(string last);
  45. char getGender();
  46. void setgender(char gen);
  47. int getDependents();
  48. void setDependents(int dep);
  49. void setDependents(string dep);
  50. double getAnnualSalary();
  51. void setAnnualSalary(double salary);
  52. void setAnnualSalary(string salary);
  53. static int getNumEmployees();
  54. static int numEmployees;
  55. };
  56. class iEmployee abstract
  57. {
  58. public:
  59. virtual double calculatePay() = 0;
  60. };
  61. void displayDivider(string outputTitle)
  62. {
  63. cout << "**************** " + outputTitle + " ****************" << endl;
  64. }
  65. Employee::Employee()
  66. {
  67. firstName = "Not Given";
  68. lastName = "Not Given";
  69. gender ='U';
  70. dependents = 0;
  71. annualSalary = 20000;
  72.  
  73. cout << "Please Enter The Employee's First Name: ";
  74. cin >> firstName;
  75. cout << "Please Enter The Employee's Last Name: ";
  76. cin >> lastName;
  77. cout << "Please Enter The Employee's Gender: ";
  78. cin >> gender;
  79.  
  80. if (gender == 'M' || gender == 'm')
  81. gender = 'M';
  82. else if (gender == 'F' || gender == 'f')
  83. gender = 'F';
  84. else
  85. gender = 'U';
  86.  
  87. cout << "Please Enter The Employee's Dependent Count: ";
  88. cin >> dependents;
  89.  
  90. while (dependents < 0 || dependents > 10)
  91. {
  92. cout << "Please Enter A Number Between 0 And 10: ";
  93. cin >> dependents;
  94. }
  95. cout << "Please Enter The Employee's Yearly Salary: ";
  96. cin >> annualSalary;
  97.  
  98. while (annualSalary < 20000 || annualSalary > 150000)
  99. {
  100. cout << "Please Enter The Employee's Annual Salary Between 20,000 And 150,000: ";
  101. cin >> annualSalary;
  102. }
  103.  
  104. }
  105. Employee::Employee(string first, string last, char gen, int dep, double salary)
  106. {
  107. firstName = first;
  108. lastName = last;
  109. gender = gen;
  110. dependents = dep;
  111. annualSalary = salary;
  112. }
  113. benefit::benefit(string health, double life, int vacation)
  114. {
  115. healthInsurance = health;
  116. lifeInsurance = life;
  117. vacationDays = vacation;
  118. }
  119. string Employee::getFirstName()
  120. {
  121. return firstName;
  122. }
  123. void Employee::setFirstName(string first)
  124. {
  125. firstName = first;
  126. }
  127. string Employee::getLastName()
  128. {
  129. return lastName;
  130. }
  131. void Employee::setLastName(string last)
  132. {
  133. lastName = last;
  134. }
  135. char Employee::getGender()
  136. {
  137. return gender;
  138. }
  139. void Employee::setgender(char gen)
  140. {
  141. gender = gen;
  142. }
  143. int Employee::getDependents()
  144. {
  145. return dependents;
  146. }
  147. void Employee::setDependents(int dep)
  148. {
  149. dependents = dep;
  150. }
  151. void Employee::setDependents(string dep)
  152. {
  153. dependents = atoi(dep.c_str());
  154. }
  155. double Employee::getAnnualSalary()
  156. {
  157. return annualSalary;
  158. }
  159. void Employee::setAnnualSalary(double salary)
  160. {
  161. annualSalary = salary;
  162. }
  163. void Employee::setAnnualSalary(string salary)
  164. {
  165. annualSalary = atof(salary.c_str());
  166. }
  167. double Employee::calculatePay()
  168. {
  169. return annualSalary/52;
  170. }
  171. int Employee::getNumEmployees()
  172. {
  173. return numEmployees;
  174. }
  175.  
  176. string benefit::getHealthInsurance()
  177. {
  178. return healthInsurance;
  179. }
  180. void benefit::setHealthInsurance(string healthIns)
  181. {
  182. healthInsurance = healthIns;
  183. }
  184. double benefit::getLifeInsurance()
  185. {
  186. return lifeInsurance;
  187. }
  188. void benefit::setLifeInsurance(double lifeIns)
  189. {
  190. lifeInsurance = lifeIns;
  191. }
  192. int benefit::getVacationDays()
  193. {
  194. return vacationDays;
  195. }
  196. void benefit::setVacationDays(int vacation)
  197. {
  198. vacationDays = vacation;
  199. }
  200.  
  201. void Employee::displayEmployee()
  202. {
  203. cout << "Employee Information" << endl;
  204. cout << "_________________________________________________ __________________________________________" << endl;
  205. cout << endl;
  206. cout << "First Name: " << firstName << endl;
  207. cout << "Last Name: " << lastName<< endl;
  208. cout << "Gender: " << gender << endl;
  209. cout << "Dependent(s): " << dependents << endl;
  210. cout << "Annual Salary: " << setprecision(2) << showpoint << fixed << annualSalary << endl;
  211. cout << "Weekly Pay: " << calculatePay() << endl;
  212. }
  213. void benefit::displayBenefit()
  214. {
  215. cout << "Health Insurance Type: " << healthInsurance << endl;
  216. cout << "Life Insurance Amount: " << lifeInsurance << endl;
  217. cout << "Vacation Days: " << vacationDays << " days" << endl;
  218. }
  219. int Employee::numEmployees = 0;
  220. int main()
  221. {
  222.  
  223. int numEmpl;
  224.  
  225. cout << "Welcome to your first Object Oriented Programming -- Employee ClassCIS247C, Week 2 Lab" << endl;
  226. cout << "NAME: Brandon Braaten" <<endl;
  227.  
  228. displayDivider("Employee One");
  229.  
  230.  
  231. Employee one;
  232. Employee::numEmployees++;
  233.  
  234. one.displayEmployee();
  235.  
  236. numEmpl = Employee::getNumEmployees();
  237. cout << "--- Number Of Employee Object Created ---" << endl;
  238. cout << "Number Of Employees: " << numEmpl << endl;
  239.  
  240. displayDivider("Employee Two");
  241.  
  242. Employee two("Mary", "Noia", 'F', 5, 24000.00);
  243. Employee::numEmployees++;
  244.  
  245. two.displayEmployee();
  246.  
  247. numEmpl = Employee::getNumEmployees();
  248. cout << "--- Number Of Employee Object Created ---" << endl;
  249. cout << "Number Of Employees: " << numEmpl << endl;
  250.  
  251. cout << "The end of the CIS 247C Week 6 iLab." << endl;
  252.  
  253. system ("pause");
  254. return 0;
  255. }
Feb 8 '12 #1
7 3315
Banfa
9,065 Expert Mod 8TB
What is your evidence for the benefit class "running" first?

The code you have post does not compile which makes it hard to see what is happening when it runs. Post the code you are compiling so we can compile it and then see what happens when it is run if you are getting a problem at runtime.
Feb 8 '12 #2
@Banfa
Here is a copy of my code.I just ran it and it is working.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. // ========== Non-class Methods ==========
  9. void displayDivider(string outputTitle)
  10. {
  11.     cout << "**************** " + outputTitle + " ****************" << endl;
  12. }
  13.  
  14.  
  15.  
  16.  
  17.  
  18. // ========== Class ==========
  19. class benefit
  20. {
  21. private:
  22.     string healthInsurance;
  23.     double lifeInsurance;
  24.     int vacationDays;
  25.     string PPO;
  26.     string HMO;
  27. public:
  28.     benefit();
  29.     benefit(string health, double life, int vacation);
  30.     void displayBenefit();
  31.     string getHealthInsurance();
  32.     void setHealthInsurance(string healthIns);
  33.     double getLifeInsurance();
  34.     void setLifeInsurance(double lifeIns);
  35.     int getVacationDays();
  36.     void setVacationDays(int vacation);
  37.  
  38.     void BenefitInput(); // earl 
  39. };  // class benefit
  40.  
  41.  
  42. benefit::benefit(string health, double life, int vacation)
  43. {
  44.     healthInsurance = health;
  45.     lifeInsurance = life;
  46.     vacationDays = vacation; 
  47. }
  48.  
  49. string benefit::getHealthInsurance()
  50. {
  51.     return healthInsurance;
  52. }
  53. void benefit::setHealthInsurance(string healthIns)
  54. {
  55.     healthInsurance = healthIns;
  56. }
  57. double benefit::getLifeInsurance()
  58. {
  59.     return lifeInsurance;
  60. }
  61. void benefit::setLifeInsurance(double lifeIns)
  62. {
  63.     lifeInsurance = lifeIns;
  64. }
  65. int benefit::getVacationDays()
  66. {
  67.     return vacationDays;
  68. }
  69. void benefit::setVacationDays(int vacation)
  70. {
  71.     vacationDays = vacation;
  72. }
  73.  
  74. void benefit::displayBenefit()
  75. {
  76.     cout << "Health Insurance Type: " << healthInsurance << endl;
  77.     cout << "Life Insurance Amount: " << lifeInsurance << endl;
  78.     cout << "Vacation Days: " << vacationDays << " days" << endl;
  79. }
  80.  
  81.  
  82. benefit::benefit()
  83. {
  84.     cout << "Please Enter the Health Insurance Type PPO or HMO: ";
  85.     cin >>  healthInsurance;
  86.     if (healthInsurance == "PPO" || healthInsurance == "ppo")
  87.             healthInsurance = "PPO";
  88.     else if (healthInsurance == "HMO" || healthInsurance == "hmo")
  89.             healthInsurance = "HMO";
  90.     else
  91.             healthInsurance = "No Health Insurance";
  92.  
  93.     cout << "Please Enter the Life Insurance Amount: ";
  94.     cin >> lifeInsurance;
  95.  
  96.     while (lifeInsurance < 0 || lifeInsurance > 1000000)
  97.     {
  98.         cout << "Please Enter A Dollar Amount Between 0 And 1000000: ";
  99.         cin >> lifeInsurance;
  100.     }
  101.  
  102.     cout << "Please Enter the Amount of Vacation Days: ";
  103.     cin >> vacationDays;
  104.  
  105.     while (vacationDays < 0 || vacationDays > 30)
  106.     {
  107.         cout << "Please Enter Vacation days Between 0 And 30: ";
  108.         cin >> vacationDays;
  109.     }
  110. }  // benefit::benefit()
  111.  
  112.  
  113.  
  114.  
  115. // ========== Class ==========
  116. class iEmployee abstract
  117. {
  118. public:
  119.     virtual double calculatePay() = 0;
  120. };  // class iEmployee
  121.  
  122.  
  123.  
  124.  
  125. // ========== Class ==========
  126. class Employee : public iEmployee
  127. {
  128. private:
  129.     string firstName;
  130.     string lastName;
  131.     char gender;
  132.     int dependents;
  133.     double annualSalary;
  134.     benefit Benefit;
  135. public:
  136.     Employee();
  137.     Employee(string first, string last, char gen, int dep, double salary);
  138.     double calculatePay();
  139.     void displayEmployee();
  140.     string getFirstName();
  141.     void setFirstName(string first);
  142.     string getLastName();
  143.     void setLastName(string last);
  144.     char getGender();
  145.     void setgender(char gen);
  146.     int getDependents();
  147.     void setDependents(int dep);
  148.     void setDependents(string dep);
  149.     double getAnnualSalary();
  150.     void setAnnualSalary(double salary);
  151.     void setAnnualSalary(string salary);
  152.     static int getNumEmployees();
  153.     static int numEmployees;
  154. };  // class Employee
  155.  
  156.  
  157.  
  158. Employee::Employee()
  159. {
  160.     firstName = "Not Given";
  161.     lastName = "Not Given";
  162.     gender ='U';
  163.     dependents = 0;
  164.     annualSalary = 20000;
  165.  
  166.     cout << "Please Enter The Employee's First Name: ";
  167.     cin >> firstName;
  168.     cout << "Please Enter The Employee's Last Name: ";
  169.     cin >> lastName;
  170.     cout << "Please Enter The Employee's Gender: ";
  171.     cin >> gender;
  172.  
  173.     if (gender == 'M' || gender == 'm')
  174.         gender = 'M';
  175.     else if (gender == 'F' || gender == 'f')
  176.         gender = 'F';
  177.     else
  178.         gender = 'U';
  179.  
  180.     cout << "Please Enter The Employee's Dependent Count: ";
  181.     cin >> dependents;
  182.  
  183.     while (dependents < 0 || dependents > 10)
  184.     {
  185.         cout << "Please Enter A Number Between 0 And 10: ";
  186.         cin >> dependents;
  187.     }
  188.     cout << "Please Enter The Employee's Yearly Salary: ";
  189.     cin >> annualSalary;
  190.  
  191.     while (annualSalary < 20000 || annualSalary > 150000)
  192.     {
  193.         cout << "Please Enter The Employee's Annual Salary Between 20,000 And 150,000: ";
  194.         cin >> annualSalary;
  195.     }
  196.  
  197. }  // Employee::Employee()
  198.  
  199.  
  200. Employee::Employee(string first, string last, char gen, int dep, double salary)
  201. {
  202.     firstName = first;
  203.     lastName = last;
  204.     gender = gen;
  205.     dependents = dep;
  206.     annualSalary = salary;
  207. }
  208.  
  209.  
  210. string Employee::getFirstName()
  211. {
  212.     return firstName;
  213. }
  214. void Employee::setFirstName(string first)
  215. {
  216.     firstName = first;
  217. }
  218. string Employee::getLastName()
  219. {
  220.     return lastName;
  221. }
  222. void Employee::setLastName(string last)
  223. {
  224.     lastName = last;
  225. }
  226. char Employee::getGender()
  227. {
  228.     return gender;
  229. }
  230. void Employee::setgender(char gen)
  231. {
  232.     gender = gen;
  233. }
  234. int Employee::getDependents()
  235. {
  236.     return dependents;
  237. }
  238. void Employee::setDependents(int dep)
  239. {
  240.     dependents = dep;
  241. }
  242. void Employee::setDependents(string dep)
  243. {
  244.     dependents = atoi(dep.c_str());
  245. }
  246. double Employee::getAnnualSalary()
  247. {
  248.     return annualSalary;
  249. }
  250. void Employee::setAnnualSalary(double salary)
  251. {
  252.     annualSalary = salary;
  253. }
  254. void Employee::setAnnualSalary(string salary)
  255. {
  256.     annualSalary = atof(salary.c_str());
  257. }
  258. double Employee::calculatePay()
  259. {
  260.     return annualSalary/52;
  261. }
  262. int Employee::getNumEmployees()
  263. {
  264.     return numEmployees;
  265. }
  266.  
  267. void Employee::displayEmployee()
  268. {
  269.     cout << "Employee Information" << endl;
  270.     cout << "___________________________________________________________________________________________" << endl;
  271.     cout << endl;
  272.     cout << "First Name: " << firstName << endl;
  273.     cout << "Last Name: " << lastName<< endl;
  274.     cout << "Gender: " << gender << endl;
  275.     cout << "Dependent(s): " <<  dependents << endl;
  276.     cout << "Annual Salary: " << setprecision(2) << showpoint << fixed << annualSalary << endl;
  277.     cout << "Weekly Pay: " << calculatePay() << endl;
  278.     cout << endl;
  279.     cout << "Benefit Information" << endl;
  280.     cout << "___________________________________________________________________________________________" << endl;
  281.     Benefit.displayBenefit();
  282.  
  283.     cout << endl;
  284. }
  285.  
  286.  
  287. int Employee::numEmployees = 0;
  288.  
  289.  
  290.  
  291.  
  292.  
  293. // ========== Main ==========
  294. int main()
  295. {
  296.  
  297.     int numEmpl;
  298.  
  299.     cout << "Welcome to your first Object Oriented Programming -- Employee ClassCIS247C, Week 2 Lab" << endl;
  300.     cout << "NAME: Brandon Braaten" <<endl;
  301.  
  302.     displayDivider("Employee One");
  303.  
  304.  
  305.     Employee one;
  306.     Employee::numEmployees++;
  307.  
  308.     one.displayEmployee();
  309.  
  310.     numEmpl = Employee::getNumEmployees();
  311.     cout << "--- Number Of Employee Object Created ---" << endl;
  312.     cout << "Number Of Employees: " << numEmpl << endl;
  313.  
  314.     displayDivider("Employee Two");
  315.  
  316.     Employee two("Mary", "Noia", 'F', 5, 24000.00);
  317.     Employee::numEmployees++;
  318.  
  319.     two.displayEmployee();
  320.  
  321.     numEmpl = Employee::getNumEmployees();
  322.     cout << "--- Number Of Employee Object Created ---" << endl;
  323.     cout << "Number Of Employees: " << numEmpl << endl;
  324.  
  325.     cout << "The end of the CIS 247C Week 6 iLab." << endl;
  326.  
  327.     system ("pause");
  328.  
  329.     return 0;
  330. }  // main()
Feb 8 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
You have created an Employee objct in main(). One of the data members of Employee is a benefit. Therefore, a benefit object needs to be created in order to create your Employee.
Feb 8 '12 #4
Banfa
9,065 Expert Mod 8TB
The code seems to compile an work.

It is not clear what you mean by "Why does my benefit class run in my program without me calling it" if you can explain in more detail what your query is we can answer it, assuming that weaknessforcats hasn't already told you what you need to know.
Feb 9 '12 #5
Hey Banfa,
Thank you for responding. Here is what I mean as you can see after running my program the first question that is asked is "Please Enter the Health Insurance type PPO or HMO:", then Life insurance, and then vacation days. Health Insurance, life insurance, and vacation days are in my benefit class. If you take a look at my main you can see that I do not have anything calling my benefits class. I have Employee one to call my Employee class in my main. I have had several people look at my code, even my professor, and no one can figure out why. I am hoping that you will be able to see where I went wrong. What should be happening when I run my program is that I should be asking for your first name, last name, etc. Then the question about your health insurance, life insurance and vacation days.
Feb 9 '12 #6
Banfa
9,065 Expert Mod 8TB
OK it is as weaknessforcats says your Employee class has a benefit class as a data member. When a class is constructed the following happens
  1. Memory is allocated
  2. The constructors of all the base classes of the class is called
  3. All the data members of the class are constructed, their constructors are called
  4. The classes constructor is call

It is important that things happen in this order so that when the constructor of your class is running the class is complete (all base classes and data members constructed)

Since benefit is a data member of Employee when you construct an instance of the Employee class it constructs an instance of the benefit class before running the Employee constructor. Since the benefit class constructor outputs to the screen and requests input from the user you see that first.

Generally it is not very good practice to put so much processing inside a constructor, the constructor should do the minimum possible to make a working class that you can call methods on.
Feb 9 '12 #7
Thank you for your help it is now working. If I need anymore help I will be back.
Feb 10 '12 #8

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

Similar topics

1
by: Josh Close | last post by:
Is there a way to call a class method without an instance of that class? class myClass: def printSomething(message): print message myClass.print() That's basically what I want to do. Is...
3
by: Christian Dieterich | last post by:
Hi, I need to create many instances of a class D that inherits from a class B. Since the constructor of B is expensive I'd like to execute it only if it's really unavoidable. Below is an example...
14
by: Timothy Madden | last post by:
Hello I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy...
5
by: PengYu.UT | last post by:
Hi, I heard that debug some C++ templates is very difficult. I'm wondering whether it is possible to compile C++ program with templates to pure C or C++ program without templates? Best...
24
by: gswork | last post by:
Let's write a c program, without knowing what it does... Some of you may recall Jim Roger's excellent series of posts (on comp.programming) exploring the implementation of common software...
16
by: leeaby | last post by:
Hi, I will appreciate your assistance. Can we write a c code which do not contain main() I have heard that this is possible. Is it really possible? Thanks for your help in advance lee
6
by: Davinci_Jeremie | last post by:
Hi Newbee here to C# I have a simple questions... In a Hello world example how does the class object Hello exist with out creating it? I come from object pascal where everything object is...
13
by: robinsonreyna | last post by:
Hi everyone Is it possible to write a program which do not have a main() function. The program should compile and run. Please give sample code to do this.
2
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello 1) I agree that useing classes is the best way to use VB.net. The question is how do you write a VB program without using classes? 2) For my own edification where can I see an actual...
2
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
"delete" does two things: 1) Invokes the destructor 2) Deallocates the memory We can manually invoke the destructor with: p->~T(); But is there any way to manually deallocate the memory...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.