Access Specifier Example in C++


Description:

In C++, there are three access specifiers: public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

Example Program:

Here is the simple program access specifier in C++.

Source Code

class Base
{
private:
    int a;
protected:
    int b;
public:
    int c;
    void funBase()
    {
        a=10;
        b=5;
        c=15;
    }
};
class Derived:public Base
{
public:
    void funDerived()
    {
       // a=10;
        b=5;
        c=15;
    }
};
int main()
{
    Base b;
   // b.a=10; Private
   // b.b=5;  Protectd
    b.c=20;
}
To download raw file Click Here


Basic Programs


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes