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
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
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
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314