943,563 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7461
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 19th, 2007
0

Parent / child class problems

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Nov 19th, 2007
0

Re: Parent / child class problems

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:

cpp Syntax (Toggle Plain Text)
  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:
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Nov 19th, 2007
0

Re: Parent / child class problems

Here is the code for my parent class.

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Nov 19th, 2007
0

Re: Parent / child class problems

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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Nov 19th, 2007
0

Re: Parent / child class problems

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
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Nov 19th, 2007
0

Re: Parent / child class problems

>> 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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Nov 19th, 2007
0

Re: Parent / child class problems

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
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Nov 19th, 2007
0

Re: Parent / child class problems

>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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 19th, 2007
1

Re: Parent / child class problems

So it's better to have base-protected-getters than protected variables?
Last edited by twomers; Nov 19th, 2007 at 5:23 pm.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Nov 19th, 2007
1

Re: Parent / child class problems

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Quick pseudocode question for anyone who might be able to help...
Next Thread in C++ Forum Timeline: create vector of a member of a class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC