In C++ any i can simulate some restricted access modifiers based on
association & inheritance?
eg,
class BombMaker{
private:
void makeAtomBomb(){
std::cout<<"mak ing atom bomb\n";
}
int getSecretFormul a()const{return 51;}
protected:
void makeRDX(){
std::cout<<"mak ing RDX\n";
}
int getRDXFormula() const{return 101;}
std::vector<Bom bMaker*helpers;
public:
void makeFireCracker (){
std::cout<<"mak ing firecrackers\n" ;
}
void makeAtomBombFor (BombMaker& m){
m.makeAtomBomb( );
}
void stealFormula(Bo mbMaker& o){
int formula = o.getSecretForm ula();
}
};
here,
BombMaker* bad = new BombMaker();
BombMaker* notSoBad = new BombMaker();
bad->makeAtomBombFo r(*notSoBad);/// i want this to work as now it
works. so another bomb maker can give the bad one to make a bomb for
him.
bad->stealFormula(* notSoBad);/// but bad one even can steal the
formula for the bomb through association with notSoBad. That should
not be allowed as the formula is only for personal use and is
dangerous for world. Can private be made exclusive so that association
is not allowed for private?
ie, essentially getSecretFormul a() can only be used by the object
itself.
secondly,
i have a derived class,
class DestructiveBudd y : public BombMaker{
public:
void getFormula(Bomb Maker& o){//1
int formula = o.getRDXFormula ();
}
void getFormulaFrom( DestructiveBudd y o){
int formula = o.getRDXFormula ();
}
void acquireFormula( ){
int formula = getRDXFormula() ;
}
void getFromHelpers( ){//2
for(std::size_t i = 0; i< helpers.size(); ++i){
helpers[i]->getRDXFormula( );
}
}
};
now is there any way to have a protected modifier which allows an
DestructiveBudd y to getFromula from a BombMaker also (as he himself is
a BombMaker) as in (1) along with other DestructiveBudd y's.
In the same way, an DestructiveBudd y object also want's all of the
helper which it has, to give the RDXFormula to him as all of them are
BombMaker and so is DestructiveBudd y himself, but not to anyone who is
not a BombMaker.
So unlike the previous case, where private through association is not
required, here protected through association is require.
Thanks
association & inheritance?
eg,
class BombMaker{
private:
void makeAtomBomb(){
std::cout<<"mak ing atom bomb\n";
}
int getSecretFormul a()const{return 51;}
protected:
void makeRDX(){
std::cout<<"mak ing RDX\n";
}
int getRDXFormula() const{return 101;}
std::vector<Bom bMaker*helpers;
public:
void makeFireCracker (){
std::cout<<"mak ing firecrackers\n" ;
}
void makeAtomBombFor (BombMaker& m){
m.makeAtomBomb( );
}
void stealFormula(Bo mbMaker& o){
int formula = o.getSecretForm ula();
}
};
here,
BombMaker* bad = new BombMaker();
BombMaker* notSoBad = new BombMaker();
bad->makeAtomBombFo r(*notSoBad);/// i want this to work as now it
works. so another bomb maker can give the bad one to make a bomb for
him.
bad->stealFormula(* notSoBad);/// but bad one even can steal the
formula for the bomb through association with notSoBad. That should
not be allowed as the formula is only for personal use and is
dangerous for world. Can private be made exclusive so that association
is not allowed for private?
ie, essentially getSecretFormul a() can only be used by the object
itself.
secondly,
i have a derived class,
class DestructiveBudd y : public BombMaker{
public:
void getFormula(Bomb Maker& o){//1
int formula = o.getRDXFormula ();
}
void getFormulaFrom( DestructiveBudd y o){
int formula = o.getRDXFormula ();
}
void acquireFormula( ){
int formula = getRDXFormula() ;
}
void getFromHelpers( ){//2
for(std::size_t i = 0; i< helpers.size(); ++i){
helpers[i]->getRDXFormula( );
}
}
};
now is there any way to have a protected modifier which allows an
DestructiveBudd y to getFromula from a BombMaker also (as he himself is
a BombMaker) as in (1) along with other DestructiveBudd y's.
In the same way, an DestructiveBudd y object also want's all of the
helper which it has, to give the RDXFormula to him as all of them are
BombMaker and so is DestructiveBudd y himself, but not to anyone who is
not a BombMaker.
So unlike the previous case, where private through association is not
required, here protected through association is require.
Thanks