Parent / child class problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Parent / child class problems

 
0
  #1
Nov 19th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is online now Online
Posting Virtuoso

Re: Parent / child class problems

 
0
  #2
Nov 19th, 2007
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:

  1. class thing {
  2. private:
  3. int priv_int;
  4.  
  5. protected:
  6. int prot_int
  7. };
  8.  
  9. class inh_thing : public thing {
  10. public:
  11. void display( void ) {
  12. // std::cout<< priv_int << "\n"; // ERROR! It's private so you don't have permission to access it.
  13. std::cout<< prot_int << "\n"; // OK
  14. }
  15. };
  16.  
  17. int main( ) {
  18. inh_thing th;
  19. th.display();
  20.  
  21. return 0;
  22. }

Didn't test this so it might be buggy...
Last edited by twomers; Nov 19th, 2007 at 5:13 pm. Reason: Heh. Forgot to do the inheritance :rolleyes:
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: Parent / child class problems

 
0
  #3
Nov 19th, 2007
Here is the code for my parent class.

  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. #ifndef EMPLOYEE_H
  8. #define EMPLOYEE_H
  9.  
  10. class Employee
  11. {
  12. private:
  13. string firstName;
  14. string lastName;
  15. string employeeID;
  16.  
  17.  
  18. public:
  19.  
  20. Employee();
  21. Employee(string, string, string);
  22. void display_object();
  23. };
  24.  
  25. Employee::Employee()
  26. {
  27. firstName = "unknown";
  28. lastName = "unknown";
  29. employeeID = "EEEEE";
  30. }
  31. Employee::Employee(string fName, string lName, string employID)
  32. {
  33. firstName = fName;
  34. lastName = lName;
  35. employeeID = employID;
  36. }
  37.  
  38. void Employee::display_object()
  39. {
  40. cout << "Employee Name: " << lastName << ", " << firstName << endl;
  41. cout << "Employee ID: " << employeeID << endl;
  42. }
  43.  
  44. #endif

and here is the code for my child class

  1. #ifndef StaffEmployee_H
  2. #define StaffEmployee_H
  3.  
  4. #include "Employee.h"
  5.  
  6.  
  7.  
  8. class StaffEmployee:public Employee
  9. {
  10. private:
  11. float annualSalary;
  12. bool outsourced;
  13. int taxBracket;
  14.  
  15.  
  16. public:
  17. StaffEmployee();
  18. StaffEmployee(string, string, string, float, bool, int);
  19. void display();
  20. };
  21.  
  22. StaffEmployee::StaffEmployee()
  23. {
  24. annualSalary = 0;
  25. outsourced = false;
  26. taxBracket = 1;
  27. }
  28. StaffEmployee::StaffEmployee(string fName, string lName, string employID, float aSalary, bool sourced, int taxB):Employee(fName, lName, employID)
  29. {
  30. firstName = fName;
  31. lastName = lName;
  32. employeeID = employID;
  33. annualSalary = aSalary;
  34. outsourced = sourced;
  35. taxBracket = taxB;
  36. }
  37.  
  38. void StaffEmployee::display()
  39. {
  40. // cout << firstName << endl;
  41. //cout << lastName << endl;
  42. //cout << employeeID << endl;
  43. cout << annualSalary << endl;
  44. cout << outsourced << endl;
  45. cout << taxBracket << endl;
  46. }
  47.  
  48. #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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is online now Online
Posting Virtuoso

Re: Parent / child class problems

 
0
  #4
Nov 19th, 2007
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.
Last edited by twomers; Nov 19th, 2007 at 5:14 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: Parent / child class problems

 
0
  #5
Nov 19th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is online now Online
Posting Virtuoso

Re: Parent / child class problems

 
0
  #6
Nov 19th, 2007
>> 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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: Parent / child class problems

 
0
  #7
Nov 19th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Parent / child class problems

 
0
  #8
Nov 19th, 2007
>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:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Employee {
  5. protected:
  6. const std::string& first_name() const
  7. {
  8. return _first_name;
  9. }
  10.  
  11. const std::string& last_name() const
  12. {
  13. return _last_name;
  14. }
  15.  
  16. void first_name ( const std::string& init )
  17. {
  18. _first_name = init;
  19. }
  20.  
  21. void last_name ( const std::string& init )
  22. {
  23. _last_name = init;
  24. }
  25. private:
  26. std::string _first_name;
  27. std::string _last_name;
  28. };
  29.  
  30. class StaffEmployee: public Employee {
  31. public:
  32. StaffEmployee ( const std::string& first, const std::string& last )
  33. {
  34. first_name ( first );
  35. last_name ( last );
  36. }
  37.  
  38. friend std::ostream& operator<< ( std::ostream& out, const StaffEmployee& emp )
  39. {
  40. return out<< emp.last_name() <<", "<< emp.first_name();
  41. }
  42. };
  43.  
  44. int main()
  45. {
  46. StaffEmployee emp ( "Julienne", "Walker" );
  47.  
  48. std::cout<< emp <<'\n';
  49. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,859
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is online now Online
Posting Virtuoso

Re: Parent / child class problems

 
1
  #9
Nov 19th, 2007
So it's better to have base-protected-getters than protected variables?
Last edited by twomers; Nov 19th, 2007 at 5:23 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Parent / child class problems

 
1
  #10
Nov 19th, 2007
>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.
Last edited by Narue; Nov 19th, 2007 at 5:35 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC