Thread: Private Inheritance Question

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Private Inheritance Question

    Hi All

    I have one question. I was trying to test private inheritance and saw something which I need some help to understand.

    Code:
    class Base
    {
    
    int age;
    
    public:
       Base(int x): age(x) {}
    
       int getAge()
       {
    	   return age;
       }
    };
    
    class Derived : private Base
    {
      string name;
    
    public:
      Derived(string x,int y):  Base(y),name(x) {}
    
    
      void getName()
     {
    	  cout<<"Name is="<<name<<"And Age is="<<getAge();
    	  cout<<age; // Compiler error as Age no accessible
     }
    
    };
    
    int main()
    {
    	B b;
    	b.get();
    	 Derived d("nickman",12);
    	 d.getName();
    	 return 0;
    }
    So I assume that in private inheritance, all base class data members and functions inherited as private to Derived class and can be access via Derived class functions.

    getAge() call from Derived class works fine but compiler give error when I accessed "age" data member.

    So question is, if we have inherited privately, then how getAge() call is working fine?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > So question is, if we have inherited privately, then how getAge() call is working fine?
    Because the function itself is a public member function of Base.

    > cout<<age; // Compiler error as Age no accessible
    If you want this to work, and prevent just anyone from accessing the data directly, then use the 'protected:' class qualifier.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    age is the base class is private, hence Derived can't access it. This doesn't change regardless of what type of inheritance you use--private is private. If you want the derived class to be able to access it, it must be protected or public. With private inheritance, all inherited members are made private to Derived, so the Base class members won't be accessible to any derived classes from Derived nor from the outside (because they're all private).

    You can access GetAge just fine because it's public in Base.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Protected/Private Inheritance.
    By Aslaville in forum C++ Programming
    Replies: 11
    Last Post: 12-06-2014, 05:57 PM
  2. When to use private Inheritance?
    By Bargi in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2010, 08:00 AM
  3. private or public inheritance?
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2009, 12:57 AM
  4. public/private inheritance
    By xion in forum C++ Programming
    Replies: 3
    Last Post: 06-17-2004, 06:36 PM
  5. private inheritance
    By da_fall_guy in forum C++ Programming
    Replies: 3
    Last Post: 04-04-2002, 06:58 AM

Tags for this Thread