private inheritance

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

private inheritance

 
0
  #1
Oct 11th, 2005
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?
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. class Animal
  5. {
  6. public:
  7. Animal() { std::cout << "Animal Constructor....\n"; }
  8. virtual ~Animal() { std::cout << "Animal Deconstructor...\n"; }
  9.  
  10. void print() const { std::cout << "Print method...\n"; }
  11. private:
  12. void printP() const { std::cout << "Private print method...\n"; }
  13. };
  14.  
  15.  
  16. class Dog : public Animal
  17. {
  18. public:
  19. Dog() { std::cout << "Dog constructor...\n"; }
  20. ~Dog() { std::cout << "Dog deconstructor..\n"; }
  21. };
  22.  
  23. class Bird : private Animal
  24. {
  25. public:
  26. Bird() { std::cout << "Bird constructor...\n"; }
  27. ~Bird() { std::cout << "Bird deconstructor...\n"; }
  28. };
  29.  
  30.  
  31.  
  32. int main()
  33. {
  34. Bird b;
  35. b.print();
  36. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,597
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: private inheritance

 
0
  #2
Oct 11th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: private inheritance

 
0
  #3
Oct 11th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: private inheritance

 
1
  #4
Oct 11th, 2005
no its not legal. because you have used private inheritance your class now looks like this.....
  1. class Bird
  2. {
  3. public:
  4. Bird();
  5. virtual ~Bird();
  6. private:
  7. void Print()const;
  8. };
now do you see why your code fails.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: private inheritance

 
0
  #5
Oct 11th, 2005
I see now why it didn't compile, but what's the purpose of private inheritance? Is it impossible to implement that method?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: private inheritance

 
0
  #6
Oct 11th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,443
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: private inheritance

 
0
  #7
Oct 11th, 2005
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC