I have a base class, say Employee
with some methods on it. I will later derive some child classes like Manager
, Developer
, Designer
etc which are employees as well (because of inheritence). Now say the code looks like -
#include <iostream>
#include <vector>
class Employee{
private : char name[5] = "abcd";
void allDept(){ std::cout<<"Woo"; }
public: void tellName(){std::cout << name << "\n"; }
void showEveryDept(){std::cout<< "Employee can see every dept\n";
allDept(); }
virtual ~Employee() {}
};
class Manager: public Employee{
private : char dept[5] = "aaaa";
public: void showOwnDept(){std::cout<< "Manager can see own dept\n";}
};
class Designer: public Employee{
private : char color = 'r';
public: void showOwnDept(){std::cout<< "Designer can see own dept\n";}
};
int main(){
Employee *E = new Designer;
E->showEveryDept();
// E->showOwnDept(); // will not work, but can be casted dynamically and even statically if sure, to call it!
Designer* D = dynamic_cast<Designer*>(E);
D->showOwnDept();
}
So what we can see here is that I can cast it and using polymorphism, point the base class pointer to derived class object and still call base class accessible methods on child class. Also to call child class methods from child class, I can dynamically cast it back, right.
But now what I want to do is, hide one of the public class member from child class invocation, so that child class isn't able to call it but base class object can. Take the example of showEveryDept()
, which can be invoked by both child as well parent classes. But since Designer and Manager have been allocated their dept, I don't want them to access this function.
I tried a very hacky way to solve this, by writing another layer of class b/w Employee class and it's child, like this -
class Employee{
private : char name[5] = "abcd";
void allDept(){ std::cout<<"Woo"; }
public: void tellName(){std::cout << name << "\n"; }
void showEveryDept(){std::cout<< "Employee can see every dept\n";
allDept();}
virtual ~Employee() {}
};
class ELayer: private Employee{
private: using Employee::showEveryDept;
private: using Employee::tellName;
};
class Manager: public ELayer{
private : char dept[5] = "aaaa";
public: void showOwnDept(){std::cout<< "Manager can see own dept\n";}
};
class Designer: public ELayer{
private : char color = 'r';
public: void showOwnDept(){std::cout<< "Designer can see own dept\n";}
};
int main(){
Employee *E = new Designer;
E->showEveryDept();
// E->showOwnDept(); // will not work, but can be casted dynamically
// and even statically if sure, to call it!
Designer* D = dynamic_cast<Designer*>(E);
D->showOwnDept();
}
but as clever as it looks, it doesn't works -
prog.cc: In function 'int main()': prog.cc:27:23: error: 'Employee' is an inaccessible base of 'Designer' Employee *E = new Designer;
So what are my options here? One stupid way would be to make that function virtual , but again child classes aren't forced to override it, and if they forgot to declare it, it will call parent's function?