Reputation: 109
So I have three classes which are the following:
The Person class:
#ifndef INC_7__LABOR_PERSON_H
#define INC_7__LABOR_PERSON_H
#include <string>
#include <iostream>
using namespace std;
class Person{
protected:
string lastName;
string firstName;
int dateOfBirth;
public:
Person(const string &lastName, const string &firstName, int dateOfBirth);
virtual void print(ostream& os = cout) const; //a virtualtol polimorfikus lehet.
};
ostream& operator <<(ostream& os, const Person&);
#endif //INC_7__LABOR_PERSON_H
The constructor for Person:
Person::Person(const string &lastName, const string &firstName, int dateOfBirth) : lastName(lastName),
firstName(firstName),
dateOfBirth(dateOfBirth) {}
The Employee class:
#ifndef INC_7__LABOR_EMPLOYEE_H
#define INC_7__LABOR_EMPLOYEE_H
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
class Employee : public Person{
protected:
string position;
static int counter;
int id;
public:
Employee(const string &lastName, const string &firstName, int dateOfBirth, const string &position);
Employee(const string &lastName, const string &firstName, int dateOfBirth, int id);
virtual void print(ostream& os = cout) const;
int getId() const;
};
#endif //INC_7__LABOR_EMPLOYEE_H
The constructor for Employee:
Employee::Employee(const string &lastName, const string &firstName, int dateOfBirth, const string &position)
: Person(lastName, firstName, dateOfBirth), position(position), id(counter++){}
The Manager class:
#ifndef INC_7__LABOR_MANAGER_H
#define INC_7__LABOR_MANAGER_H
#include "Employee.h"
#include <vector>
using namespace std;
class Manager : public Employee{
private:
vector<Employee*> employees;
public:
Manager(const string &lastName, const string &firstName, int dateOfBirth, const string &position);
static string MANAGER_POSITION;
virtual void print(ostream& os = cout) const;
void addEmployee(Employee*);
void deleteEmployee(int);
};
#endif //INC_7__LABOR_MANAGER_H
The constructor for Manager:
string Manager::MANAGER_POSITION="manager";
Manager::Manager(const string &lastName, const string &firstName, int dateOfBirth, const string &position)
: Employee(lastName, firstName, dateOfBirth, position) {}
Now my problem is that I cannot reach the id variable through the manager:
void Manager::deleteEmployee(int id)
{
for(int i = 0; i < employees.size(); ++i)
{
if(employees.at(i)->id == id) //HERE the "employees.at(i)->id" is the problem.
{
employees.erase(employees.begin() + i);
}
}
}
Although the id variable is protected the program cannot access it. I solved the problem by using a getter getId(), but I would like to know why I cannot access it without getter.
The program was written in c++14 with cygwin compiler.
Upvotes: 1
Views: 34
Reputation: 118300
The short answer here is that this is because that's how inheritance in C++ works.
Your Manager
class is attempting to access a member of an Employee
class that it inherits as protected
from Person
. It is a separate Employee
object at hand, and it's trying to get at its protected member.
Something that's protected
cannot be accessed from other classes. It is true that Manager
inherits from Employee
, but that only means that Manager
, in its own class only, can access all of the protected
and public
fields that it inherits.
Your code example can be reduced to a much shorter, bare bones, single compilable example (please take some effort to do the same in your next question, after reading stackoverflow's requirements for a minimal reproducible example, because it will help others understand your question better):
class a {
protected:
int b;
};
class c : public a {
};
class d : public c {
public:
int method();
};
c *p;
d *q;
int d::method()
{
return b; // This is ok
return p->b; // This is an error
return q->b; // But this is ok
}
Even though d
inherits from c
, they are still two distinct classes, and if d
is accessing a distinct c
object, it cannot access its protected members.
Upvotes: 1