Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,
Is there any way to create a scenario as below -

========================================
 Class A
{
 void fun();
};


'Class B' is derived from 'Class A'
and 'Class C' is derived from 'Class B'

=========================================

Can we make "fun()" method only accessible to 'Class C' and restrict 'Class B' to access fun() though it is derived from 'Class A' ??

Actually one interviewer had asked me this question.
If its a valid question. then please guide me.Thanks :)
Posted
Updated 29-May-11 12:23pm
v3
Comments
Sergey Alexandrovich Kryukov 2-Jun-11 3:04am    
Yes, this question is adequate and interesting, unlike your most recent questions (also interview?).
Sorry, some interview questions can be idiotic. I can imagine working with such people...
--SA

What you describe is routinely schema of using inheritance: A <- B <- C. It does not even require C++ multiple inheritance:

C++
class A {
public:
    void Fun() {}
};

class B : public A {
};

class C : public B {
};


You cannot limit access to the instance function A.Fun though. It would be possible if this function was virtual and overridden in B. In this case, you can limit access and cannot make access more open.

You can formally declare private Fun, but it would not work as expected as it would be the different Fun with the same name hiding the inherited Fun. It could be called anyway, consider this:

C++
class A {
public:
    void Fun() {}
};

class B : public A {
private:
    //bad idea:
    void Fun() {} //no! this is a new function!
};

//this is how to overcome hiding:
void TestHiding() {
   B b;
   //not accessible, but this is a different Fun:
   //b.Fun(); //no fun (pun intended)

   //how to work around:
   A *b_as_a = &b;
   b_as_a->Fun(); //it calls inherited Fun
}


If this question was difficult to you, you're not yet ready to do any C++ development using OOP. Step back and learn you C++ course and related topics.

—SA
 
Share this answer
 
v3
Comments
Monjurul Habib 29-May-11 18:26pm    
suitable..my 5.
Sergey Alexandrovich Kryukov 29-May-11 18:37pm    
Thank you, Monjurul.
--SA
Emilio Garavaglia 30-May-11 9:06am    
A b_as_a = b actually is another A containing a copy of the A in B.
Wouldn't you mean A& b_as_a = b; ?!?
Sergey Alexandrovich Kryukov 30-May-11 15:20pm    
Sure, I simply forgot this is not .NET reference... :<
I added the variant with pointer, to make it correct; now b_as_a points to b but compile-time allows to call a hidden function.
Please see the updated answer.
Thank you for your correction.
--SA
Stefan_Lang 30-May-11 11:51am    
Your remark about using virtual functions and overriding isn't quite correct, because if you override virtual void A::func() you wouldn't be calling A::func() anymore

But see my rresponse below.
If you want to make sure that only the function fun() can be accessed like this, then you can achieve this with the help of an additional base class:
class Base {
protected:
   virtual void fun()=0;
};
class A : private Base { // private inheritance prevents access from derived classes
private:
   void fun(); // implements Base::fun()
};
class B : public A {
   // can not access Base::fun() because A::Base is private
   // can not access A::fun(), because A::fun() is private
};
class C : public virtual B, public virtual Base {
   // using multiple inheritance, C can now access Base::fun()
   // virtual inheritance required to make sure there is just one Base object inherited from
   void foo() {
      Base::fun(); // calls A::fun()
   }
};
 
Share this answer
 
Comments
Espen Harlinn 30-May-11 16:27pm    
Fair enough, my 5 - a bit more work, a bit more fun? pun fully intentional :)
Stefan_Lang 31-May-11 3:43am    
If you can't have fun, override it with your own! ;)
I guess you mean 'Class B' should not have access to fun :)
C++
class A
{
friend class C;
 void fun();
};

class B : public A
{
};

class C : public B
{
public:
 void more_fun()
 {
  // Have some fun
  fun(); 
 }
};


Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 29-May-11 16:55pm    
Correct, my 5. Please see my answer -- more detail on the topic.
--SA
Espen Harlinn 30-May-11 14:45pm    
Thank you, SAKryukov!
Monjurul Habib 29-May-11 18:27pm    
my 5, good enough.
Espen Harlinn 30-May-11 14:46pm    
Thank you, Monjurul!
Ashish Tyagi 40 29-May-11 23:25pm    
Sufficient, my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900