0

I'm fairly new to programming and I'm having this problem that I can't solve. I've tried all I can think of. I'm prepared for it to be such a simple mistake.

main.cpp

#include <iostream>
#include <iomanip>
#include "new_employee.h"
#include "new_employee.cpp"
#include "permanent_employee.cpp"

using namespace std;

int in_employee[4] = {101, 102, 103, 104};
int in_bankaccount[4] = {80045001, 80045002, 80045003, 80045004};
float in_hours[4] = {40, 50, 50, 51};
float in_rate[4] = {22, 22, 24, 26};

int main()
{
    for(int i=0;i<4;i++)
{
    new_employee employee[i](in_employee[i], in_bankaccount[i]);
}
///permanent_employee employee2(in_employee[1], in_bankaccount[1]);
///permanent_employee employee3(in_employee[2], in_bankaccount[2]);
///permanent_employee employee4(in_employee[3], in_bankaccount[3]);
}

new_employee.h

#if !defined NEW_EMPLOYEE
#define NEW_EMPLOYEE

class new_employee
{
public:
    new_employee();
    new_employee(int employee_number, int account_number);
private:
    int employee_no, account_no;
    float hourly_rate, hours_worked;
};

class permanent_employee : public new_employee
{
public:
    permanent_employee();
    permanent_employee(int employee_number, int account_number);
private:
    float union_deduction, vhi_deduction;
};
#endif

new_employee.cpp

#include <iostream>
#include <iomanip>
#include "new_employee.h"

using namespace std;

new_employee::new_employee()
{
    employee_no = 0;
    account_no = 0;
}

new_employee::new_employee(int employee_number, int account_number)
{
    employee_no = employee_number;
    account_no = account_number;
}

permanent_employee.cpp

#include <iostream>
#include <iomanip>
#include "new_employee.h"

using namespace std;

permanent_employee::permanent_employee()
{
    employee_no = 0;
    account_no = 0;
}

permanent_employee::permanent_employee(int employee_number, int account_number)
{
    employee_no = employee_number;
    account_no = account_number;
}

So, I haven't even tried to properly run the program for its original function because of the following error copied directly from Codeblocks.

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In constructor 'permanent_employee::permanent_employee()':|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::employee_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|9|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::account_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|10|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h||In constructor 'permanent_employee::permanent_employee(int, int)':|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::employee_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|15|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\new_employee.h|10|error: 'int new_employee::account_no' is private|

Z:\C++\Assignment 3 - Payroll Processing\permanent_employee.cpp|16|error: within this context|

Z:\C++\Assignment 3 - Payroll Processing\main.cpp||In function 'int main()':|

Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|error: variable-sized object 'employee' may not be initialized|

Z:\C++\Assignment 3 - Payroll Processing\main.cpp|18|warning: unused variable 'employee' [-Wunused-variable]| ||=== Build finished: 9 errors, 1 warnings (0 minutes, 0 seconds) ===|

I'm trying to make a base class new_employee with a derived class permanent_employee. To me it looks like each is trying to access the other's variables. I would appreciate any feedback.

Thank you for your help.

PS. I'm new to this website so I am sorry if I have posted incorrectly.

Jordan
  • 3
  • 1
  • 5
    You might want to follow a [good C++ book](http://stackoverflow.com/q/388242/1782465) to get a solid grasp of C++ semantics and concepts such as inheritance. It will give you a better foundation than piecewise questions on Stack Overflow. – Angew is no longer proud of SO Mar 16 '17 at 13:40
  • 5
    Never include *.cpp files in other files – CinCout Mar 16 '17 at 13:41
  • How do you understand the error message "In constructor `permanent_employee::permanent_employee()`: `int new_employee::employee_no` is private"? – Angew is no longer proud of SO Mar 16 '17 at 13:41
  • 2
    The error is pretty clear. private variables and functions cannot be used in a derived class. Perhaps you want protected? You should also be explicitly calling the base class constructor from the derived one. – Retired Ninja Mar 16 '17 at 13:42
  • what are you trying to say by the line: `new_employee employee[i](in_employee[i], in_bankaccount[i]);`? especially `employee[i]` part – Andriy Tylychko Mar 16 '17 at 14:05
  • 2
    another "never" advice: never use `using namespace std;` – Andriy Tylychko Mar 16 '17 at 14:05
  • @Andy T I second the "never use `using namespace std;`" advice. I hate how so many sites on the internet and C++ books have this as the kind of defacto standard. – soulsabr Mar 16 '17 at 14:50

2 Answers2

2

Change:

permanent_employee::permanent_employee(int employee_number, int account_number)
{
    employee_no = employee_number;
    account_no = account_number;
}

To call the base class constructor:

permanent_employee::permanent_employee(int employee_number, int account_number)
 : new_employee(employee_number, account_number)
{
}

Since the member variables are declared private for new_employee, they are not accessible even by derived classes. You could declare them as protected if you'd like derived classes to be able to modify them (but sometimes you don't for reasons like invariant preservation).

AndyG
  • 39,700
  • 8
  • 109
  • 143
1

You are having problems with the accessibility of your member variables. In general the accessibility goes like this:

Public : Anybody and everybody can see and change these guys. Protected : The class that contains these variables as members and any derived class can change these. Outside classes cannot access them. Private: Only the class that contains these member variables can alter or use them in any way.

The error is caused by the permanent_employee class trying to access the private members of the new_employee class. You could also try calling the base class constructor from the derived constructor.

Either way you go I highly, HIGHLY recommend you take some time to fully understand the differences between public, protected, and private member variables and functions before you do anything else. This will make your life tons easier in the long run.

soulsabr
  • 895
  • 4
  • 16
  • I might be misunderstanding where you're coming from (and inheritance in general). I'm trying to have a class 'new_employee' with X variables and a derived class 'permanent_employee' with X + Y variables. I'm trying to change the 'permanent_employee' 's copy of the X variables that it should have inherited from 'new_employee' – Jordan Mar 16 '17 at 13:55
  • 1
    @Jordan It did inherit the variables it just can't access them due to permissions. Private means ONLY the declaring class can access them; `new_employee`. Inherited classes cannot. Protected means only the declaring and inherited classes can access them; `new_employee` and/or `permanent_employee`. Try switching from private to protected and you'll see. But, like I said, this is a very critical concept to understand and you should take the time to read and experiment with it before moving on with your learning. – soulsabr Mar 16 '17 at 14:01