It possible to inherit the private member in drived class?

Showing Answers 1 - 28 of 28 Answers

Sasan

  • May 14th, 2006
 

yes, if the derived class is inheriting from base class with private access level

class derived : private base

  Was this answer useful?  Yes

Mahendar

  • May 22nd, 2006
 

No...not at all...

we cannot acces a private member of base class by inheriting as private or even as public.....

we can acces unless and untill we have something like freind class

swarn kant

  • May 25th, 2006
 

No it is not possible untill and unless we use the keyworld friend

Arup

  • May 28th, 2006
 

      Hey u can not do it by deriving as private .it narrows down the access .even the protected and public members in the base class becomes private

sumitnagpal

  • Jul 24th, 2006
 

Its a basic questionwhen u derive publically...private member become private for the derived class...so they can be accessed inside derived class.

  Was this answer useful?  Yes

Balamurugan Erulandi

  • Aug 10th, 2006
 

Base class private members can't dervied to the Derived class even if the inheritance is public or private or protected.We can use friend() function to access private data members of the Baseclass

Mandar Atre

  • Aug 29th, 2006
 

No,it is not possible to inherit the private memeber of a class from a derived class.To inherit the private memeber of a class you have to first create a Friend Class and with the help of this you can do that.  

anitha

  • Oct 9th, 2006
 

We can never inherit the private members in a derived class..If we wish to do then the variable should be declared in "Protected" section of a base class.So the members will be inherited by the immediate derived class.

ndarshan

  • Oct 24th, 2006
 

No u can never give aceess to the derived class of ur private variable ...if so then the whole point of data encapsulation is spoilt .....but u can make use of it in the pubic functions of ur class and then make derived class to make use of it thts where a proper c++ programer life becomes miserable....heheheheh...bcoz he needs to give a limited acess to the code not directly but indirectly.....but when u inherit private acess ...I guess it is possible its by using the c++ feature called "Granting access " to only public and protect variable....but not for sure to private ...tht is u can make the public and protected variables get access though they become private as the inheritance is private type ....syntax :public: //inside the derived classbase:: int i; //where i is the public or protectd variable of base class but looses acceblity by inheriting it //as private so ur giving the access back to the drived clas.....ths is somr kind of //reconcidraton rgdDarshan.Nfeel free to mail me at darshan_2004@rediffmail.com

  Was this answer useful?  Yes

zigzag

  • Jan 17th, 2007
 

Private members of base class are always inherited to derived classes. Just that u cant access them directly (and just that all books say tha "No u cant !!!"). To prove this, take a base class with an integer as the private member. Take a derived class with member as another integer. Create object for both and check the sizes of those objects. If base object shows 4 bytes as the size then derived object will show 8. This is because pvt member of base is also inherited.
U can access that too. Take a pointer to derived class. Typecast it to base type pointer and move it with ++. U can print values if u have initialized it. Drawback of having pointer in C++? :-)

  Was this answer useful?  Yes

phani dasam

  • Mar 29th, 2007
 

Yes..it is possible to inherit the private members of the base class. But they cannot be accessible. Their scope is still private .. whatever may be the mode of inheritance.

Thanks

  Was this answer useful?  Yes

R.S.JAYASATHYANARAYANAN

  • May 9th, 2007
 

private members of base class cannot access directly by derived class but we can access the data member indirectly that using the derived class member functions and access specifier must be public in derived class.
for example

class a
{

    int a;
    public :
          void getdata()
          {
                      cout<<"enter the value";
                      cin>>a;
          }
          int putdata()
          {
                    return a;
            }
};
class b:public a //here acccess specifier must b e public not private
{
        public :
               
                      void display()
                      {
                                  getdata();
                                  cout<<"The value of a is "<                      }
}
void main()
{
b s;
  s.display();
}

  Was this answer useful?  Yes

R.S.JAYASATHYANARAYANAN

  • May 12th, 2007
 

private members of base class cannot access directly by derived class but we can access the data member indirectly that using the derived class member functions and access specifier must be public in derived class.
for example

class a
{

    int a;
          void getdata()
          {
                      cout<<"enter the value";
                      cin>>a;
          }
          int putdata()
          {
                    return a;
            }
};
class b:public a //here acccess specifier must b e public not private
{
        public :
               
                      void display()
                      {
                                  getdata();
                                  cout<<"The value of a is "<                      }
}
void main()
{
b s;
  s.display();
}

  Was this answer useful?  Yes

Inheritance is one thing, access is quite another. For a C++ class,
1. Private members - are off limites to everyone except the ones that are declared in one way or the other, inside the class (direct members or friends). This holds true in every case without exceptions - inheritance or not.
2. Public members - can be accessed globally by any function/method across the application, so long as there is an object of the class instantiated. In case of inheritance, public members are available to the derived class in a manner the derivation took place (private, public, or protected).
3. Protected members - these are specifically used to facilitate inheritance. Their behaviour is exactly same as the private members as far as the class in which they are declared is concerned. All rules that apply to private members apply. However, the difference is clarified when inheritance is used. These are the members available to the derived class. Again, their behaviour in the derived class depends on the manner in which the derivation took place (private, public, protected).

So, the short answer is
1. If you want to access parent class' private members from a derived class, provide a public access method in the parent class.
2. If you want the derived class to inherit a parent class' private members, make the private members in the parent class as protected. In this case, if the child class is derived private, parent protected members behave like private in the child class, if the derivation is public, they act as public, and if the derivation is protected, they act as protected.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions