| | |
private inheritance
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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)
#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(); }
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
no its not legal. because you have used private inheritance your class now looks like this.....
now do you see why your code fails.
C++ Syntax (Toggle Plain Text)
class Bird { public: Bird(); virtual ~Bird(); private: void Print()const; };
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: C++ vector question?
- Next Thread: Help w/ for loop.
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






