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
             Animal() { std::cout << "Animal Constructor....\n"; }
             virtual ~Animal() { std::cout << "Animal Deconstructor...\n"; }
             void print() const { std::cout << "Print method...\n"; }
              void printP() const { std::cout << "Private print method...\n"; }

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

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

int main()
    Bird b;

Recommended Answers

All 7 Replies

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.

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?

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

class Bird
      virtual ~Bird();
      void Print()const;

now do you see why your code fails.

commented: Good Stuff +3

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.