Passing data to a class.?.?

In a program I am editing there is a ProductionWorker class and an Employee class. Production Worker seems to be inherited from the EMployee class.
Employee class consist of employees name, number, and date hired. In the main function these instructions are listed:
Create ProductionWorker object by passing all the employee's data as arguments to the constructor.

I am unsure how to pass the data on.

Last edited on
1
2
3
4
5
6
class ProductionWorker : public Employee  // Inherit from Employee
{
public:
  ProductionWorker (const Employee & e) : Employee (e)  // Constructor takes an employee and initializes inherited class
  {}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Chapter 15, Programming Challenge 1: Employee and ProductionWorker classes
#include <iostream>
#include <iomanip>
#include "ProductionWorker.h"

using namespace std;
// Function prototype 
void displayInfo(ProductionWorker);
int main() {     
	
	// Create ProductionWorker object using default constructor
	ProductionWorker worker1;

				
	// Call displayInfo to print ProductionWorker's contents
	displayInfo(worker1);

				
	// Create ProductionWorker object by passing all the employee's data as arguments to the constructor
	ProductionWorker worker2(e);


				
	// Call displayInfo to print ProductionWorker's contents
	displayInfo(worker2);
	return 0;


}
//****************************************************** 
// The displayInfo function displays a production     * 
// worker's employment information.                   * 
//****************************************************** 
void displayInfo(ProductionWorker e) 
{ 
	cout << setprecision(2) << fixed << showpoint;


cout << "Name: " << e.getName() << endl;
cout << "Employee number: " << e.getNumber() << endl;
cout << "Hire date: " << e.getHireDate() << endl;
//
cout << "Shift: " // 
<< e.getShiftName() << endl;
				  // optional
//cout << "Shift number: "  << e.getShiftNumber() << endl;
cout << "Pay rate: " 


<< e.getPayRate() << endl;


}


THis is what I have so far in main. I don't think I have a grasp on this.

EDIT: Also, in the displayInfo method I am supposed to be accessing methods from the Employee class as well. How do I go about getting access to these. Right now there are just a bunch of errors.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Specification file for the Employee class 
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
using namespace std;
class Employee {
private:    
	// Employee name   
	string name;
	// Employee number 
	int employeeNum;
	// Hire date
	string dateHired;
	
public:


	// Default constructor
	Employee();
	// Constructor
	Employee(string n, int num, string d);
	// 3 Mutators
	void setName(string n);
	void setEmployeeNum(int num);
	void setDateHired(string d);
	// 3 Accessors
	string getName();
	int getNumber();
	string getHireDate();


};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Specification file for the ProductionWorker Class 
#ifndef PRODUCTION_WORKER_H 
#define PRODUCTION_WORKER_H 

#include <iostream>
#include "Employee.h"
using namespace std;
class ProductionWorker : public Employee{
private:     // The worker's shift    
	string shift;
			 // The worker's hourly pay rate
	double payRate;
public:


	// Default constructor 
	ProductionWorker();
	// Constructor 
	ProductionWorker() : Employee(e);
	// 2 Mutators 
	void setShiftName(string s);
	void setPayRate(double p);
	// 2 Accessors 
	string getShiftName();
	double getPayRate();
	// optional getShiftName()
}; 
#endif 


These are my two header files. I am digging myself deeper here and confusing myself. I was hesitant to post some code but not im desperate lol. For these simple functions do I even need to create cpp files. Or can these be contained within header files. BTW this still does not run. any guidance is appreciated
I never did find a solution for this one. I am trying to pass the object to the display info function to display such information. I am getting errors.
EDIT:
okay got it to print the default onctructors. But when I try to implement this as suggested I am getting
1
2
3
4
ProductionWorker(const Employee &e) : Employee (e)
{
	
}

I get this decleration has no storage or class type specifier
Last edited on
On line 19, you're along the right lines - you're specifying a base class (i.e. Employee) constructor in the initialisation list of the derived class (ProductionWorker) constructor:

18
19
	// Constructor 
	ProductionWorker() : Employee(e);


However, there are a few things wrong with it. Firstly, there are no arguments to the ProductionWorker constructor, which means you're declaring a default constructor. But you've already declared a default constructor at line 17 - you can't have two different constructors with the same signature.

If the purpose is to have a ProductionWorker constructor that passes data through to it's base class constructor, then you want to have the ProductionWorker constructor take that data as parameters, so it can pass them through to the Employee constructor.

Secondly, initialisation lists are used in the definition of a method, not the declaration.

Thirdly, look at that initialisation list. What is e in this context? There's no definition of anything called e, and nor is there any constructor defined for Employee that only takes a single argument.

Your initialisation list should invoke the base class constructor that you want it to invoke. The arguments to the Employee constructor will be the arguments that were passed into the ProductionWorker constructor.

You can find more about this, and examples, in the tutorials on this site:

http://www.cplusplus.com/doc/tutorial/inheritance/
1
2
3
ProductionWorker(const Employee &e) : Employee(e) {
	Employee();
	}


Hows this?
I will take a look at the tutorial again, I am still confused on what I am supposed to accomplish with this function.
Last edited on
Okay will It seems not I have access to the employee class through the ProductionWorker Objects created in main. This seems right. Unless I am missing something else.
Hows this?

Line 2: You don't want to call Employee's default constructor here. You've already called Employee's parameterized constructor on line 1.
So simply delete it? I am not working on it yet but will here in a moment
ALso. I have another project I am working on that's similar.
1
2
3
4
5
6
7
8
9
10
11
// Create a CustomerData object and pass arguments    
	// to the constructor.
	CustomerData customer1;
	
	// Display the object's data.    
	cout << "Customer #1\n"         
		<< "-----------\n";   
	displayCustomer(customer1);       
	
	// Create another CustomerData object using the default    
	// constructor and mutator functions. 


If someone could explain this to me. I know I just did this in the above exercise, but I just used the mutators to manipulate the data through main. Whats actually the difference here...
Last edited on
1
2
3
ProductionWorker(const Employee &e) : Employee(e) {
	Employee();
	}

Hows this?

The line where you just call the default constructor of Employee is pointless - all it does is create a temporary unnamed Employee object that will then be destroyed. What are you trying to achieve with that line?

Apart from that, what you've written will work, because the ProductionWorker constructor will invoke the copy constructor for Employee, that your compiler will have created by default (since you didn't define one).

I can't help wondering - is this really the way you want to do this? Right now, the way this would work is that you have to create an Employee object, and then create another ProductionWorker object that copies its base class data from that first Employee object. Is that really what you've been asked to do?

Okay will It seems not I have access to the employee class through the ProductionWorker Objects created in main. This seems right. Unless I am missing something else.

The ProductionWorker object is an Employee object, so it contains that data. However, since the data members of Employee are (quite rightly) declared as private, you can't access them directly from ProductionWorker methods. To do that, you'll need to call the get* and set* methods you've defined.


Yea this is confusing the hell out of me. The instructions you see above are all im working with. Any suggestions are appreciated.

http://www.learncpp.com/cpp-tutorial/114-constructors-and-initialization-of-derived-classes/

^^ just a reference for me once I leave work today.
Last edited on
So, looking at this:

Create ProductionWorker object by passing all the employee's data as arguments to the constructor.


it doesn't seem to be telling you to create an Employee object, and then pass that into the constructor. Rather it looks as though it's asking you to pass in the data that the Employee object needs - in other words:

string n, int num, string d

So it seems to me that those are the arguments you need your ProductionWorker constructor to take, and to pass through to the Employee constructor.

Thanks for ignoring all my questions, by the way. It really makes me feel like it's been worth taking time and effort to try and contribute to your thread.
Topic archived. No new replies allowed.