Inheritance and vectors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 27
Reputation: n8thatsme is an unknown quantity at this point 
Solved Threads: 0
n8thatsme n8thatsme is offline Offline
Light Poster

Inheritance and vectors

 
0
  #1
Nov 27th, 2008
Ok, I never really understood vectors and I have write a program that uses a vector of Employee references to store the company’s payroll. My program will load the payroll by reading a file (employee.dat) that contains one employee’s information per line. Each line of the file will begin with a letter (S for salaried, H for hourly, C for commission, or B for base plus commission.) The fields on the remainder of the line will depend on the first letter of the line and will be separated by spaces. I didn't think you could use a vector of references but here is the code. Also, I need to know how to read it in from a file and to use the appropriate object. Should I use switch statements or else ifs? Remember, its just modifying it.

  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. using std::fixed;
  5.  
  6. #include <iomanip>
  7. using std::setprecision;
  8.  
  9. #include <vector>
  10. using std::vector;
  11.  
  12. // include definitions of classes in Employee hierarchy
  13. #include "Employee.h"
  14. #include "SalariedEmployee.h"
  15. #include "HourlyEmployee.h"
  16. #include "CommissionEmployee.h"
  17. #include "BasePlusCommissionEmployee.h"
  18.  
  19. int main()
  20. {
  21. // create derived-class objects
  22. SalariedEmployee salariedEmployee(
  23. "John", "Smith", "111-11-1111", 800 );
  24. HourlyEmployee hourlyEmployee(
  25. "Karen", "Price", "222-22-2222", 16.75, 40 );
  26. CommissionEmployee commissionEmployee(
  27. "Sue", "Jones", "333-33-3333", 10000, .06 );
  28. BasePlusCommissionEmployee basePlusCommissionEmployee(
  29. "Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
  30.  
  31. // create vector of four base-class pointers
  32. vector < Employee * > employees( 4 );
  33.  
  34. // initialize vector with Employees
  35. employees[ 0 ] = &salariedEmployee;
  36. employees[ 1 ] = &hourlyEmployee;
  37. employees[ 2 ] = &commissionEmployee;
  38. employees[ 3 ] = &basePlusCommissionEmployee;
  39.  
  40. return 0;
  41. } // end main

These are all in there own header file associated with there name.
  1. Employee( const string &, const string &, const string & );
  2.  
  3. //constructor definition
  4. Employee::Employee( const string &first, const string &last,
  5. const string &ssn )
  6. : firstName( first ), lastName( last ), socialSecurityNumber( ssn )
  7. {
  8. // empty body
  9. } // end Employee constructor

  1. HourlyEmployee( const string &, const string &,
  2. const string &, double = 0.0, double = 0.0 );
  3.  
  4. // constructor definition
  5. HourlyEmployee::HourlyEmployee( const string &first, const string &last,
  6. const string &ssn, double hourlyWage, double hoursWorked )
  7. : Employee( first, last, ssn )
  8. {
  9. setWage( hourlyWage ); // validate hourly wage
  10. setHours( hoursWorked ); // validate hours worked
  11. } // end HourlyEmployee constructor

  1. SalariedEmployee( const string &, const string &,
  2. const string &, double = 0.0 );
  3.  
  4. // constructor definition
  5. SalariedEmployee::SalariedEmployee( const string &first,
  6. const string &last, const string &ssn, double salary )
  7. : Employee( first, last, ssn )
  8. {
  9. setWeeklySalary( salary );
  10. } // end SalariedEmployee constructor

  1. CommissionEmployee( const string &, const string &,
  2. const string &, double = 0.0, double = 0.0 );
  3.  
  4. // constructor defintion
  5. CommissionEmployee::CommissionEmployee( const string &first,
  6. const string &last, const string &ssn, double sales, double rate )
  7. : Employee( first, last, ssn )
  8. {
  9. setGrossSales( sales );
  10. setCommissionRate( rate );
  11. } // end CommissionEmployee constructor

  1. BasePlusCommissionEmployee( const string &, const string &,
  2. const string &, double = 0.0, double = 0.0, double = 0.0 );
  3.  
  4. //constructor definition
  5. BasePlusCommissionEmployee::BasePlusCommissionEmployee(
  6. const string &first, const string &last, const string &ssn,
  7. double sales, double rate, double salary )
  8. : CommissionEmployee( first, last, ssn, sales, rate )
  9. {
  10. setBaseSalary( salary ); // validate and store base salary
  11. } // end BasePlusCommissionEmployee constructor
Last edited by n8thatsme; Nov 27th, 2008 at 5:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Inheritance and vectors

 
0
  #2
Nov 27th, 2008
Is your Instructor's name Ron, by chance?

-Alex
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Inheritance and vectors

 
0
  #3
Nov 27th, 2008
1. You can't declare a vector of references in C++ (it's the other story why). Fortunately, I see a vector of POINTERS in you snippet. Feel the difference
2. Why pointers in that case? Better define std::vector<SalariedEmployee> . Take on trust: it's much more better for you
3. What's a problem? Read a file with getline(ifstream,string) line by line, check up the 1st char (with switch or if, it's a matter of taste), then attach the line to istringstream and extract field by field to SalariedEmployee object members then push_back result into the vector. That's all...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 27
Reputation: n8thatsme is an unknown quantity at this point 
Solved Threads: 0
n8thatsme n8thatsme is offline Offline
Light Poster

Re: Inheritance and vectors

 
0
  #4
Nov 27th, 2008
I'm guessing this guy was a bad instructor perhaps? No, my instructor isn't Ron, his name is Terry and he was a Software Engineer for 27 years.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Inheritance and vectors

 
0
  #5
Nov 27th, 2008
Originally Posted by n8thatsme View Post
I'm guessing this guy was a bad instructor perhaps? No, my instructor isn't Ron, his name is Terry and he was a Software Engineer for 27 years.
By no means am I attempting to discredit any Instructor. The assignment is familiar to me - very similar to what I had to do in a C++ class.

-Alex
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 27
Reputation: n8thatsme is an unknown quantity at this point 
Solved Threads: 0
n8thatsme n8thatsme is offline Offline
Light Poster

Re: Inheritance and vectors

 
0
  #6
Nov 27th, 2008
Originally Posted by ArkM View Post
1. You can't declare a vector of references in C++ (it's the other story why). Fortunately, I see a vector of POINTERS in you snippet. Feel the difference
Yeah I feel the difference lol.
2. Why pointers in that case? Better define std::vector<SalariedEmployee> . Take on trust: it's much more better for you
I have to use Employee because it is the base class, so I can just use Employee instead of SalariedEmployee? Also, I didn't mention this but after my program has read in the file, it should go into a “query” mode. In this mode, it should prompt for a social security number to be located and the current month (as an integer). It should then search for a match of the SS number and retrieve the current earnings. In addition, if the employee’s birth month matches the current month, I will add a $100 birthday bonus to the earnings. Then I will display the employee’s name, birthdate, and earnings including any bonus. So I need to use pointers still correct?
3. What's a problem? Read a file with getline(ifstream,string) line by line, check up the 1st char (with switch or if, it's a matter of taste), then attach the line to istringstream and extract field by field to SalariedEmployee object members then push_back result into the vector. That's all...
Will this be different now that I've explained what I have to do after I read the file?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Inheritance and vectors

 
0
  #7
Nov 27th, 2008
Oh yes, I did not pay attention to (potential) polymorphism in your code. Yes, you need a vector of pointers to a base class.
The only difference: create a proper derived class object by operator new and assign the obtained pointer to a base class pointer variable then push_back this value into the vector (and don't forget to traverse the vector and delete all objects before exit).
Yet another tip: if you want to search objects by unique SS numbers, consider std::map<SS_number_type,Employee*> container instead std::vector. You will get a very fast (and free of charge) std::map search by map::find member function.
Last edited by ArkM; Nov 27th, 2008 at 7:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 27
Reputation: n8thatsme is an unknown quantity at this point 
Solved Threads: 0
n8thatsme n8thatsme is offline Offline
Light Poster

Re: Inheritance and vectors

 
0
  #8
Nov 27th, 2008
Ok, so here is what I did so far. Like I said I'm not used to vectors so its pretty new to me. When you said for me to create using new do you mean like this?
  1. salariedEmployee = new Employee;
  2. salariedEmployee = new SalariedEmployee;
As for the push_back command, I'm not sure how to use that. Here is my code. Do I create the new objects inside each case? If your wondering what the input will look like from the file it will be as follows.
S firstName lastName socialSecurityNumber weeklySalary birthdate
H firstName lastName socialSecurityNumber wage hours birthdate
C firstName lastName socialSecurityNumber grossSales commissionRate birthdate
B firstName lastName socialSecurityNumber grossSales commissionRate baseSalary birthdate

S Tom Smith 111-11-1111 800 09/10/1942
H Karen Price 222-22-2222 16.75 40 01/01/1982
C Sue Jones 333-33-3333 10000 .06 05/31/1980
B Bob Lewis 444-44-4444 5000 .04 300 10/04/1950


  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6. // include definitions of classes in Employee hierarchy
  7. #include "Date.h"
  8. #include "Employee.h"
  9. #include "SalariedEmployee.h"
  10. #include "HourlyEmployee.h"
  11. #include "CommissionEmployee.h"
  12. #include "BasePlusCommissionEmployee.h"
  13. using namespace std;
  14.  
  15. int main()
  16. {
  17. vector < Employee * > employees;
  18. string firstName;
  19. string lastName;
  20. string ssn;
  21. double salary;
  22. double hourlyWage;
  23. double hoursWorked;
  24. double sales;
  25. double rate;
  26. int month;
  27. int day;
  28. int year;
  29. char type;
  30. fstream indata; // The file to be opened
  31.  
  32. indata.open("employee.txt"); // Open the file to be read
  33. if(!indata)
  34. {
  35. cout << "Error: file could not be opened" << endl;
  36. exit(1);
  37. }
  38.  
  39. do
  40. {
  41. indata>>type;
  42. switch(type)
  43. {
  44. case 'S':
  45. indata >> firstName;
  46. indata >> lastName;
  47. indata >> ssn;
  48. indata >> salary;
  49. indata >> month;
  50. indata.ignore(1);
  51. indata >> day;
  52. indata.ignore(1);
  53. indata >> year;
  54. break;
  55. case 'H':
  56. indata >> firstName;
  57. indata >> lastName;
  58. indata >> ssn;
  59. indata >> hourlyWage;
  60. indata >> hoursWorked;
  61. indata >> month;
  62. indata.ignore(1);
  63. indata >> day;
  64. indata.ignore(1);
  65. indata >> year;
  66. break;
  67. case 'C':
  68. indata >> firstName;
  69. indata >> lastName;
  70. indata >> ssn;
  71. indata >> sales;
  72. indata >> rate;
  73. indata >> month;
  74. indata.ignore(1);
  75. indata >> day;
  76. indata.ignore(1);
  77. indata >> year;
  78. break;
  79. case 'B':
  80. indata >> firstName;
  81. indata >> lastName;
  82. indata >> ssn;
  83. indata >> sales;
  84. indata >> rate;
  85. indata >> salary;
  86. indata >> month;
  87. indata.ignore(1);
  88. indata >> day;
  89. indata.ignore(1);
  90. indata >> year;
  91. break;
  92. }
  93. }while(!indata.eof());
  94.  
  95. return 0;
  96. }
Last edited by n8thatsme; Nov 27th, 2008 at 9:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Inheritance and vectors

 
0
  #9
Nov 28th, 2008
what he meant was
base* b = new child();

then push_back(b) into the vector.

however for dynamic polymorphism you should keep functions which need to be derived from base class as virtual, so that when you call them with the base class pointer it calls the correct child class function.

so basically you need to read about polymorphism and 'dynamic' polymorphism in particular.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Inheritance and vectors

 
0
  #10
Nov 28th, 2008
Nope, that's wrong approach. Right code sceleton looks like:
  1. Employee* p;
  2. string line;
  3. istringstream sstr;
  4. char key;
  5. bool goon = true;
  6. ...
  7. while (goon && getline(indata,line)) {
  8. sstr.str(line); // attach new line to sstr
  9. sstr.seekg(0); // read sstr from the beginning
  10. sstr >> key;
  11. switch (key) {
  12. case 'S':
  13. ... // get S... fields from sstr
  14. p = new salariedEmployee(...);
  15. v.push_back(p);
  16. break;
  17. ...
  18. default: // bad key
  19. goon = false; // then print error message
  20. break;
  21. }
  22. }
But at first let's know about you employees zoo (see Agni's post).
What did you want to do with all these classes? What's base class definition?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC