Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>what are Access Specifiers?</h2> <p>There are 3 <code>access specifiers</code> for a class/struct/Union in C++. These access specifiers define how the members of the class can be accessed. Of course, any member of a class is accessible within that class(Inside any member function of that same class). Moving ahead to type of access specifiers, they are: </p> <p><strong>Public</strong> - The members declared as Public are accessible from outside the Class through an object of the class. </p> <p><strong>Protected</strong> - The members declared as Protected are accessible from outside the class <strong>BUT</strong> only in a class derived from it. </p> <p><strong>Private</strong> - These members are only accessible from within the class. No outside Access is allowed.</p> <p>An Source Code Example:</p> <pre><code>class MyClass { public: int a; protected: int b; private: int c; }; int main() { MyClass obj; obj.a = 10; //Allowed obj.b = 20; //Not Allowed, gives compiler error obj.c = 30; //Not Allowed, gives compiler error } </code></pre> <hr> <h2>Inheritance and Access Specifiers</h2> <p>Inheritance in C++ can be one of the following types: </p> <ul> <li><strong><code>Private</code> Inheritance</strong> </li> <li><strong><code>Public</code> Inheritance</strong> </li> <li><strong><code>Protected</code> inheritance</strong> </li> </ul> <p>Here are the member access rules with respect to each of these: </p> <blockquote> <p><strong><em>First and most important rule <code>Private</code> members of a class are never accessible from anywhere except the members of the same class.</em></strong> </p> </blockquote> <h2>Public Inheritance:</h2> <blockquote> <p>All <code>Public</code> members of the Base Class become <code>Public</code> Members of the derived class &amp;<br> All <code>Protected</code> members of the Base Class become <code>Protected</code> Members of the Derived Class. </p> </blockquote> <p>i.e. No change in the Access of the members. The access rules we discussed before are further then applied to these members. </p> <p>Code Example: </p> <pre><code>Class Base { public: int a; protected: int b; private: int c; }; class Derived:public Base { void doSomething() { a = 10; //Allowed b = 20; //Allowed c = 30; //Not Allowed, Compiler Error } }; int main() { Derived obj; obj.a = 10; //Allowed obj.b = 20; //Not Allowed, Compiler Error obj.c = 30; //Not Allowed, Compiler Error } </code></pre> <h2>Private Inheritance:</h2> <blockquote> <p>All <code>Public</code> members of the Base Class become <code>Private</code> Members of the Derived class &amp;<br> All <code>Protected</code> members of the Base Class become <code>Private</code> Members of the Derived Class.</p> </blockquote> <p>An code Example: </p> <pre><code>Class Base { public: int a; protected: int b; private: int c; }; class Derived:private Base //Not mentioning private is OK because for classes it defaults to private { void doSomething() { a = 10; //Allowed b = 20; //Allowed c = 30; //Not Allowed, Compiler Error } }; class Derived2:public Derived { void doSomethingMore() { a = 10; //Not Allowed, Compiler Error, a is private member of Derived now b = 20; //Not Allowed, Compiler Error, b is private member of Derived now c = 30; //Not Allowed, Compiler Error } }; int main() { Derived obj; obj.a = 10; //Not Allowed, Compiler Error obj.b = 20; //Not Allowed, Compiler Error obj.c = 30; //Not Allowed, Compiler Error } </code></pre> <h2>Protected Inheritance:</h2> <blockquote> <p>All <code>Public</code> members of the Base Class become <code>Protected</code> Members of the derived class &amp;<br> All <code>Protected</code> members of the Base Class become <code>Protected</code> Members of the Derived Class. </p> </blockquote> <p>A Code Example:</p> <pre><code>Class Base { public: int a; protected: int b; private: int c; }; class Derived:protected Base { void doSomething() { a = 10; //Allowed b = 20; //Allowed c = 30; //Not Allowed, Compiler Error } }; class Derived2:public Derived { void doSomethingMore() { a = 10; //Allowed, a is protected member inside Derived &amp; Derived2 is public derivation from Derived, a is now protected member of Derived2 b = 20; //Allowed, b is protected member inside Derived &amp; Derived2 is public derivation from Derived, b is now protected member of Derived2 c = 30; //Not Allowed, Compiler Error } }; int main() { Derived obj; obj.a = 10; //Not Allowed, Compiler Error obj.b = 20; //Not Allowed, Compiler Error obj.c = 30; //Not Allowed, Compiler Error } </code></pre> <p>Remember the same access rules apply to the classes and members down the inheritance hierarchy. </p> <hr> <h2>Important points to note:</h2> <p><strong><em>- Access Specification is per-Class not per-Object</em></strong></p> <p>Note that the access specification C++ work on per-Class basis and not per-object basis.<br> A good example of this is that in a copy constructor or Copy Assignment operator function, all the members of the object being passed can be accessed.</p> <p><strong><em>- A Derived class can only access members of its own Base class</em></strong></p> <p>Consider the <strong><a href="http://ideone.com/5iAeM" rel="noreferrer">following code example</a></strong>:</p> <pre><code>class Myclass { protected: int x; }; class derived : public Myclass { public: void f( Myclass&amp; obj ) { obj.x = 5; } }; int main() { return 0; } </code></pre> <p>It gives an compilation error: </p> <blockquote> <p>prog.cpp:4: error: ‘int Myclass::x’ is protected</p> </blockquote> <p>Because the derived class can only access members of its <strong><em>own Base Class</em></strong>. Note that the object <code>obj</code> being passed here is no way related to the <code>derived</code> class function in which it is being accessed, it is an altogether different object and hence <code>derived</code> member function cannot access its members.</p> <hr> <h2>What is a <code>friend</code>? How does <code>friend</code> affect access specification rules?</h2> <p>You can declare a function or class as <code>friend</code> of another class. When you do so the access specification rules do not apply to the <code>friend</code>ed class/function. The class or function can access all the members of that particular class. </p> <blockquote> <p><strong><em>So do <code>friend</code>s break Encapsulation?</em></strong></p> </blockquote> <p>No they don't, On the contrary they enhance Encapsulation!</p> <p><code>friend</code>ship is used to indicate a <strong><em>intentional strong coupling</em></strong> between two entities.<br> If there exists a special relationship between two entities such that one needs access to others <code>private</code> or <code>protected</code> members but You do not want <strong><em>everyone</em></strong> to have access by using the <code>public</code> access specifier then you should use <code>friend</code>ship. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload