I am writing a program and one of my classes is called Employee the private variables for Employee class are

string firstName;
string lastName;

I have a child class called StaffEmployee and for its private variables I would like to use

int employeeID;

I want to create an object in int main() called StaffEmployee A; and when you use A.display(); the program should display the firstName lastName and employeeID. However everytime I do this I get an error message saying error C2248: 'Employee::lastName' : cannot access private member declared in class 'Employee'.

If someone could explain to me how to go about fixing this problem it would be awesome! Thanks for your help

It should be protected rather than public. The Employee members. That's all I can guess here. If that doesn't work post some code.

Pour example:

class thing {
private:
  int priv_int;

protected:
  int prot_int
};

class inh_thing : public thing {
public:
  void display( void ) {
    // std::cout<< priv_int << "\n"; // ERROR! It's private so you don't have permission to access it. 
    std::cout<< prot_int << "\n"; // OK
  }
};

int main( ) {
  inh_thing th;
  th.display();

  return 0;
}

Didn't test this so it might be buggy...

Here is the code for my parent class.

#include <iostream>
#include <string>

using namespace std;


#ifndef EMPLOYEE_H
#define EMPLOYEE_H

class Employee
{
private:
	string firstName;
	string lastName;
	string employeeID;


public:

	Employee();
	Employee(string, string, string);
	void display_object();
};
	
	Employee::Employee()
	{
		 firstName = "unknown";
		 lastName = "unknown";
		 employeeID = "EEEEE";
	}
	Employee::Employee(string fName, string lName, string employID)
	{
		firstName = fName;
		lastName = lName;
		employeeID = employID;
	}
	
	void Employee::display_object()
	{
		cout << "Employee Name:  " << lastName << ", " << firstName << endl;
		cout << "Employee ID:    " << employeeID << endl;
	}

	#endif

and here is the code for my child class

#ifndef StaffEmployee_H
#define StaffEmployee_H

#include "Employee.h"



class StaffEmployee:public Employee
{
private:
	float annualSalary;
	bool outsourced;
	int taxBracket;


public:
	StaffEmployee();
	StaffEmployee(string, string, string, float, bool, int);
	void display();
};

StaffEmployee::StaffEmployee()
	{
		annualSalary = 0;
		outsourced = false;
		taxBracket = 1;
	}
StaffEmployee::StaffEmployee(string fName, string lName, string employID, float aSalary, bool sourced, int taxB):Employee(fName, lName, employID)
	{
		firstName = fName;
		lastName = lName;
		employeeID = employID;
		annualSalary = aSalary;
		outsourced = sourced;
		taxBracket = taxB;
	}

void StaffEmployee::display()
	{
	//	cout << firstName << endl;
		//cout << lastName << endl;
		//cout << employeeID << endl;
		cout << annualSalary << endl;
		cout << outsourced << endl;
		cout << taxBracket << endl;
	}

	#endif

I am almost 100% positive I have the classes set up correctly but I still cannot get it to work

any help would be great thanks

protected:
string firstName;
string lastName;
string employeeID;

That's probably all you need to change.

I edited my first post if you didn't see it.

Twomers I will try to make it protected instead of private and see if that fixes it but I though with parent/child classes they could use each others private variables.

Thanks again for your help

>> but I though with parent/child classes they could use each others private variables.
I think that's only with private inheritance, but you did public inheritance. I never really messed with the former, so I'm not certain if what I said is accurate, but I think it is.
Protected guards the regular variables with the public identifier unless the class is inherited in which case the inherited object can access those variables.

Well changing it to protected work. I guess I just thought that you could use private variables if it were a parent child class thing. Anyway thanks again for your help twomers

>It should be protected rather than public. The Employee members.
No, it should be private. Just pretend that it's illegal to have protected data members.

>If someone could explain to me how to go about fixing this problem it would be awesome!
Add a protected member function that gives child classes access to the data member:

#include <iostream>
#include <string>

class Employee {
protected:
  const std::string& first_name() const
  {
    return _first_name;
  }

  const std::string& last_name() const
  {
    return _last_name;
  }

  void first_name ( const std::string& init )
  {
    _first_name = init;
  }

  void last_name ( const std::string& init )
  {
    _last_name = init;
  }
private:
  std::string _first_name;
  std::string _last_name;
};

class StaffEmployee: public Employee {
public:
  StaffEmployee ( const std::string& first, const std::string& last )
  {
    first_name ( first );
    last_name ( last );
  }

  friend std::ostream& operator<< ( std::ostream& out, const StaffEmployee& emp )
  {
    return out<< emp.last_name() <<", "<< emp.first_name();
  }
};

int main()
{
  StaffEmployee emp ( "Julienne", "Walker" );

  std::cout<< emp <<'\n';
}

So it's better to have base-protected-getters than protected variables?

>So it's better to have base-protected-getters than protected variables?
Yes. Think of class design as a service and a client. The service supplies a guaranteed interface and the client uses the interface to do what it needs. This is most obvious in the public interface of a class for users of that class. However, it also applies to the protected interface of a class for the children of that class. If you make the internal data protected, it means that you can't ever change how the base class works or you'll break all of the clients, just like if you make the internal data public.

so which one of these methods should i use? Which is better?

>so which one of these methods should i use? Which is better?
You're the programmer, so in the end the call is yours.

When in doubt listen to Naure :)
Makes sense. I asked the question without thinking. Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.