In an effort to reduce the number of nonconstructive comments posted by visitors who do not read the entire article or previous comments, I have decided to change the title of the article and the initial summary to something more accurate.
In C++ there are three inheritance access specifiers:
Any of these three inheritance access specifiers can be modified with the virtual keyword. In my experience interviewing candidates for c++ positions, I've learned that the average programmer does not know how these are used, or even what they mean. So I thought I would go over them here.
The three access modifiers public, protected and private are analogous to the access modifiers used for class members.
public ie: class c : public base {}; the base class can be seen by anyone who has access to the derived class. That is, any members inherited from base can be seen by code accessing c.protected ie: class c : protected base {}; the base class can only be seen by subclasses of C.private ie: class c : private base {}; the base class can only be seen by the class C itself.Examples of how this plays out:
struct X { public: void A() {} }; struct Y { public: void B() {} }; struct Z { public: void C() {} }; struct Q : public X, protected Y, private Z { public: void Test() { A(); // OK B(); // OK C(); // OK } }; struct R : public Q { public: void Test2() { A(); // OK B(); // OK C(); // NOT OK Q t; Y *y = &t // OK Z *z = &t // NOT OK } }; int main(int argc, char **argv) { Q t1; t1.A(); // OK t1.B(); // NOT OK t1.C(); // NOT OK R t2; t2.A(); // OK t2.B(); // NOT OK t2.C(); // NOT OK X *x = &t1; // OK Y *y = &t1; // NOT OK Z *z = &t1; // NOT OK x = &t2; // OK y = &t2; // NOT OK z = &t2; // NOT OK }
What about Virtual?
Oh right. Virtual is only useful when multiple inheritance is involved and the same class appears in the inheritance graph more than once. If the inheritance is declared as virtual all instances of the class are merged into one sub object and that sub object is initialized once. If the class that appears multiple times in the inheritance graph is NOT declared virtual one sub object is created for EACH instance of the class and the class is initialized multiple times.
Be careful! If the class is inherited sometimes as virtual and sometimes not, the virtual instances are merged and the non-virtual instances are not, giving you a mix of behavior.
Update
As stated in comments, these are not types of inheritance, but inheritance access specifiers. The concepts still hold. I personally use C++ in a Nutshell as my daily desk reference when programming. Access specifiers are noted on pages 313 and 314.
Comments
This is fu***** wrong
This is fu***** wrong
This is not fully wrong. Bt
This is not fully wrong. Bt these are not types of inheritance.
wrong!!
this is completely wrong...The types of inheritance are:
single inheritance
multiple inheritance
multilevel inheritance
given here are the levels of visibility(public,private and protected)
Yes yes
This is restated in the article itself and by two other commenters.
Need to put more effort
I appreciate your effort on this topic.
The example given is pretty vague... Use the one below if you like it.
Class A
{
public:
int apub;
private:
int aprv;
protected:
int apro;
}
Basic thumb rule in inheritance. You cannot inherit private members.
Public visibility mode during Inheritance
---------------------------------------------------
Public inheritance will inherit protected members,methods of base class with visibilty modifier as protected.
Public inheritance will inherit public members,methods of base class with visibilty modifier as public.
Class B : public A
{
public:
int bpub; // also has access to apub
private:
int bprv;
protected:
int bpro; // also has access to apro
}
Private visibility mode during Inheritance
---------------------------------------------------
Private inheritance will inherit protected as well as public members,methods of base class with visibilty modifier as private.
Class B : private A
{
public:
int bpub;
private:
int bprv; // also has access to apub,apro
protected:
int bpro;
}
Protected visibility mode during Inheritance
---------------------------------------------------------
Private inheritance will inherit protected as well as public members,methods of base class with visibilty modifier as protected.
Class B : private A
{
public:
int bpub;
private:
int bprv;
protected:
int bpro; // also has access to apub,apro
}
Cheers,
Sanjay
Very Clear
Your explanation is very good, and it is very easy to understand.
how do u take interviews?
dude..
i dont know how the hell u interview other candidates wen u dont know these simple concepts
try reading c++ for beginners..
I shed a tear
Jason, if you are interviewing candidates I shed a tear for the future of your company.
Go back to the basics and stop pretending you know C++.
Come on..
As an experienced programmer, I must say that I am sick and tired of the egos in this field. Come on guys, do you really have to mock and banter someone for asking a question or posting some comments? Don't be a$$es. Truth is, most people who think they are the smartest programmers on earth can't design good software for sh*t. All talk.
Lets make things better
Why reinvent the wheel - there is a perfectly good description; Check it out!
http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
Actually
My article was posted fully 1.5 years before the one you linked to, so... the linkage is actually reinventing the wheel. And really, we are all reinventing what the C++ FAQ Lite has done for a while with their vast base of C++ info:
http://www.parashift.com/c++-faq-lite/private-inheritance.html
-Jason
Regarding Inheritance
Hi Jason.
Firstly, Very simply we can conclude this discussion as
Types of inheritance:-
1. Single Inheritance ---- class A : public class B{}
2. Multiple ------------------Class A : public class B,public Class c {}
3. Multilevel -----------------Class A derived from Base class B,Whereas Class B derived from Base Class C
4.Hybrid --- Combine any two inheritance mentioned above like multiple-multilevel
5. Hierarchical--- One super class and many sub classes
Now Access Specifier (Inheritance)
------------------------------------------------------------------------------------------------------------------------------------
Base Class Types of Inheritance
Public Protected Private
Public Public Protected Private
Protected Protected Protected Private
Private Not /Access Not /Access Not /Access
Thanks
Update
Base Class if public
Public in base: Public
Private in base: Not accessible in derived
Protected in base: Protected
Base Class if protected
Public in base: protected
Private in base: Not accessible in derived
Protected in base: Protected
Base Class if private
Public in base: private
Private in base: Not accessible in derived
Protected in base: private