954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

private inheritance

I may not be understanding private inheritance right, but I thought when you inherited privately that you could still access public member data/functions, but the child class of the derived class would not. For some reason it is saying the public print() method is not accessable in this context....Is there something I'm doing wrong?

#include <iostream>
#include <cstdlib>

class Animal
{
      public:
             Animal() { std::cout << "Animal Constructor....\n"; }
             virtual ~Animal() { std::cout << "Animal Deconstructor...\n"; }
             
             void print() const { std::cout << "Print method...\n"; }
      private:
              void printP() const { std::cout << "Private print method...\n"; }
};


class Dog : public Animal
{
      public: 
              Dog() { std::cout << "Dog constructor...\n"; }
              ~Dog() { std::cout << "Dog deconstructor..\n"; }
};

class Bird : private Animal
{
      public:
             Bird() { std::cout << "Bird constructor...\n"; }
             ~Bird() { std::cout << "Bird deconstructor...\n"; }
};



int main()
{
    Bird b;
    b.print();
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

your compiler is correct. print() is not accessible outside Bird class because of private inherentence. It can only be called from within Bird class just like any other private member.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I'm using bloodshed....It's giving me the same error but I don't understand why. Is it not legal for me to do this?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

no its not legal. because you have used private inheritance your class now looks like this.....

class Bird
{
   public:
      Bird();
      virtual ~Bird();
   private:
      void Print()const;
};

now do you see why your code fails.

Stoned_coder
Junior Poster
164 posts since Jul 2005
Reputation Points: 19
Solved Threads: 5
 

I see now why it didn't compile, but what's the purpose of private inheritance? Is it impossible to implement that method?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

private inheritance has its uses. It is similar in effect to composition and nothing like public inheritance. When you become more familiar with c++, you will often find uses for private inheritance.

Stoned_coder
Junior Poster
164 posts since Jul 2005
Reputation Points: 19
Solved Threads: 5
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

since Print P function is declared as private in base class, u can not acess that function even if u inherit it as public.
now make print p function as public in base class and using Animal::printP will solve u r problem

brkurre
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You