| | |
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 |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






