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...
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
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
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
>> 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.
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
>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';
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
So it's better to have base-protected-getters than protected variables?
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>so which one of these methods should i use? Which is better?
You're the programmer, so in the end the call is yours.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
When in doubt listen to Naure :)
Makes sense. I asked the question without thinking. Cheers.
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57