944,068 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12742
  • C++ RSS
Oct 11th, 2005
0

private inheritance

Expand Post »
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?
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Oct 11th, 2005
0

Re: private inheritance

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,955 posts
since Aug 2005
Oct 11th, 2005
0

Re: private inheritance

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?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Oct 11th, 2005
1

Re: private inheritance

no its not legal. because you have used private inheritance your class now looks like this.....
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Oct 11th, 2005
0

Re: private inheritance

I see now why it didn't compile, but what's the purpose of private inheritance? Is it impossible to implement that method?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Oct 11th, 2005
0

Re: private inheritance

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.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Oct 11th, 2005
0

Re: private inheritance

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 12th, 2010
0
Re: 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brkurre is offline Offline
1 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Delete Node from linked list
Next Thread in C++ Forum Timeline: Semaphores and threads





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC