| | |
Parent / child class problems
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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:
Didn't test this so it might be buggy...
Pour example:
cpp Syntax (Toggle Plain Text)
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...
Last edited by twomers; Nov 19th, 2007 at 5:13 pm. Reason: Heh. Forgot to do the inheritance :rolleyes:
Here is the code for my parent class.
and here is the code for my child class
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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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.
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.
>> 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 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.
>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:
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)
#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'; }
I'm here to prove you wrong.
So it's better to have base-protected-getters than protected variables?
Last edited by twomers; Nov 19th, 2007 at 5:23 pm.
>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.
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.
![]() |
Similar Threads
- Parent/Child Windows References (JavaScript / DHTML / AJAX)
- parent/child (Java)
- ASP.NET 2.0, Parent/Child Data Control? (ASP.NET)
- Abstract class homework problem (C++)
- Having problems saving objects into an array, then accessing the data later for repo (Java)
- Need help in figuring logic for a parser (Java)
Other Threads in the C++ Forum
- Previous Thread: Quick pseudocode question for anyone who might be able to help...
- Next Thread: create vector of a member of a class
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






