Inheritance Class

I'm supposed to to make a program in which I must enter in various info about an employee and split some of said info into two classes. One being the base while the other is the derived. When putting in the info some of it acts weird for example I want to enter in the hire date, but skips that and goes to shift instead. Also, when I want to output all the data I can't for some reason. Here's what I have:
P.S the code itself is not finished because I want to figure out where I went wrong.
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
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
using namespace std;

class employee
{
public:
	void info();
	void display();
private:
	string name;
	int number;
	string hire_date;
};

class ProductionWorker :public employee
{
private:
	int shift;
	double PayRate;
public:
	void work();
	
};

void employee::info()
{
	cout << "Enter in your name: ";
	getline(cin, name);
	cout << endl;
	cout << "Enter in employee number: ";
	cin >> number;
	cout << endl;
	cout << "Enter in your date of hire (Month/day/year): ";
	getline(cin, hire_date);
	cout << endl;
}
void ProductionWorker::work()
{
	cout << "Enter in shift(day/night): ";
	cin >> shift;
	cout << endl;
	cout << "Enter in pay rate: ";
	cin >> PayRate;
}



int main()
{
	ProductionWorker enter;
	string name;
	int number;
	string hire_date;
	int shift;
	double PayRate;
	enter.info();
	enter.work();
	cout <<"Name: " <<name << endl;//name doesn't show
	cout << "Pay rate: " << PayRate << endl;//doesn't work
	
}
Last edited on
Nothing is done with name or number in main, only the name and number in enter. Get rid of your declarations of name and number in main and instead cout enter.name and enter.number.
Also put a cin.flush(); after your getline() calls.
Last edited on
Are you suggesting I should do this?
If so, it doesn't work.
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	ProductionWorker enter;
	//string name;
	//int number;
	string hire_date;
	int shift;
	double PayRate;
	enter.info();
	enter.work();
	cout << enter.name;
	
}
@highwayman, flush only works for output streams, not input.

std::getline removes the trailing newline character in the input stream, std::cin doesn't remove it. When mixing std::cin and std::getline you need to ignore the rest of the input buffer after extracting a value with std::cin.

You need to retrieve your class member data instead of using local created variables. The most common way is to create "getter" class methods.

Another way to do it (NOT RECOMMENDED!) is make your class data members public.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>

class employee
{
public:
   void GetEmployeeInfo();
   // void display();  // you haven't defined your display method yet

 // add some "getter" methods to retrieve your class member data
public:
   std::string GetName() const { return name; }
   int GetNumber() const { return number; }
   std::string GetHireDate() const { return hire_date; }

private:
   std::string name;
   int number;
   std::string hire_date;
};

class ProductionWorker : public employee
{
private:
   int shift;
   double PayRate;

public:
   void GetWorkerInfo();

public:
   int GetShift() const { return shift; }
   double GetPayRate() const { return PayRate; }
};


int main()
{
   ProductionWorker enter;
   enter.GetEmployeeInfo();
   enter.GetWorkerInfo();

   // call your getter to retrieve the name
   std::cout << "Name: " << enter.GetName() << '\n';

   std::cout << "Pay rate: " << enter.GetPayRate() << '\n';
}


void employee::GetEmployeeInfo()
{
   std::cout << "Enter in your name: ";
   std::getline(std::cin, name);
   std::cout << '\n';

   std::cout << "Enter in employee number: ";
   std::cin >> number;
   std::cout << '\n';
   std::cin.ignore(32767, '\n');  // <-- add this to "eat" the endline left in the buffer

   std::cout << "Enter in your date of hire (Month/day/year): ";
   std::getline(std::cin, hire_date);
   std::cout << '\n';
}

void ProductionWorker::GetWorkerInfo()
{
   std::cout << "Enter in shift(day [1]/ night [2]): ";
   std::cin >> shift;
   std::cout << '\n';

   std::cout << "Enter in pay rate: ";
   std::cin >> PayRate;
   std::cout << '\n';
}

Enter in your name: Joe d'Ragman

Enter in employee number: 15

Enter in your date of hire (Month/day/year): 11/10/2016

Enter in shift(day [1]/ night [2]): 1

Enter in pay rate: 12.75

Name: Joe d'Ragman
Pay rate: 12.75
Thank you for the help! I'll go on and finish what I need to now.
Topic archived. No new replies allowed.