Advertisement

Inheritance (public vs private)

Started by November 17, 2000 05:08 PM
1 comment, last by farmersckn 24 years, 1 month ago
Let''s say you have a class Polygon. class Polygon { public: // Constructor/destructor // Methods: private: point m_Vertices[vertices]; }; Now you have a class Square, which inherits from a 4 sided poly. class Square : Polygon<4> { public: // Constructor/destructor: // Methods: private: // Members: }; What''s the difference between a Square : private Polygon<4>, and a Square : public Polygon<4>. What is the advantage of a privately inherited class? farmersckn
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
quote: Original post by farmersckn


What''s the difference between a Square : private Polygon<4>,
and a Square : public Polygon<4>.
What is the advantage of a privately inherited class?


Ok, well assuming you don''t care about the syntax errors of the code...

I don''t believe that you can inherit the private members in C++. Maybe you mean ''protected'' instead of private? I guess it doesn''t really matter because it would have the same effect.

When you inherit a class with ''public'' the derived class only has access to the public elements of the base class. However, the private elements are still there. In other words, if there is a public function that works on a private data member in your base class, you can still call that function and it will work properly from your derived class. You just can''t directly access that member variable yourself.

On the other hand, if you inherit with ''protected'' you can then access anything that was public or protected in the base class from the derived class. I assume that if private can be used (which I''m not sure) it would be the same way.

It''s usually best to inherit with public to keep things modular. In general, it is just considered ''better programming practice.'' However, there are times when you might need to inherit the protected members. If you can avoid it, though, I''d recommend using public in most cases.

I hope that makes sense?
Advertisement
quote: Original post by farmersckn
What''s the difference between a Square : private Polygon<4>,
and a Square : public Polygon<4>.


Let''s say I do the following:

  class A {private:   void a() { ... }protected:   void b() { ... }public:   void c() { ... }};  class B : public A {private:   void d() { ... }protected:   void e() { ... }public:   void f() { ... }   void g()   {      a();  //illegal; private on parent      b();  //okay; protected on parent      c();  //okay; public on parent   }};  class C : private A {private:   void h() { ... }protected:   void i() { ... }public:   void j() { ... }   void k()   {      a();  //illegal; private on parent      b();  //okay; protected on parent      c();  //okay; public on parent   }};  void foo(){   A x;   B y;   C z;     x.a();  //illegal; it''s private   x.b();  //illegal; it''s protected   x.c();  //okay; it''s public     y.a();  //illegal; it''s private on public A of B   y.b();  //illegal; it''s protected on public A of B   y.c();  //okay, it''s public on public A of B   y.d();  //illegal, it''s private on B   y.e();  //illegal, it''s protected on B   y.f();  //okay, it''s public on B     z.a();  //illegal; it''s private on private A of C   z.b();  //illegal; it''s protected on private A of C   z.c();  //illegal, it''s public on PRIVATE A of C   z.h();  //illegal, it''s private on C   z.i();  //illegal, it''s protected on C   z.j();  //okay, it''s public on C}  


The public or private inheritance affects how outsiders access inherited members. The call to z.c() is a good example: it''s public on A, but because C inherits A as a private parent, outsiders cannot access the A portion of C at all.

(Note: I''m fairly sure the above is correct, but need to double-check my reference manual which isn''t handy at the moment.)


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
---- --- -- -
New York. New York. New York. Texas. Texas. New York. New York. Canada.
---- --- -- -

This topic is closed to new replies.

Advertisement