Visibility modes in C++

Visibility modes in C++

 Visibility modes in C++ refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes.

Public:

Members declared as public are accessible from any part of the program, including outside the class and its derived classes.

Public members are part of the class interface, and they can be accessed freely.


class MyClass {

public:

    int publicVar;

    void publicFunction() {

        // code here

    }

};

Private:

Members (    int privateVar)declared as private are only accessible within the class(class MyClass) they are declared in. They are not visible outside the class or in derived classes.

Private members are used to implement the internal details of the class and are not meant to be directly accessed from outside.

class MyClass {

private:

    int privateVar;

    void privateFunction() {

        // code here

    }

};

Protected:

Members declared as protected are similar to private members, but they can be accessed by derived classes.

Protected members are typically used when you want to allow derived classes to access certain internals of the base class, but still, keep them hidden from the rest of the program.

class BaseClass {

protected:

    int protectedVar;

    void protectedFunction() {

        // code here

    }

};

class DerivedClass : public BaseClass {

    // DerivedClass can access protectedVar and protectedFunction

};

Here’s an example demonstrating the usage of these visibility modes in C++:

Visibility modes Example

#include <iostream.h>

#include<conio.h>

class MyClass {

public:

    // Public members are accessible from outside the class

    int publicVar;

    // Public member function

    void publicFunction() {

        cout << “Public function called.” << endl;

    }

private:

    // Private members are only accessible within the class

    int privateVar;

    // Private member function

    void privateFunction() {

        cout << “Private function called.” << endl;

    }

protected:

    // Protected members are accessible within the class and its derived classes

    int protectedVar;

    // Protected member function

    void protectedFunction() {

        cout << “Protected function called.” << endl;

    }

};

void main()

{

clrscr();

    MyClass obj;

    // Accessing public members

    obj.publicVar = 42;

    obj.publicFunction();

    // Private members and functions are not accessible from outside the class

    // obj.privateVar = 10;  // Error: ‘int MyClass::privateVar’ is private

    // obj.privateFunction(); // Error: ‘void MyClass::privateFunction()’ is private

    // Protected members and functions are not accessible from outside the class

    // obj.protectedVar = 20;  // Error: ‘int MyClass::protectedVar’ is protected

    // obj.protectedFunction(); // Error: ‘void MyClass::protectedFunction()’ is protected

    getch();

}

Visibility modes in C++
Visibility modes in C++

In this example:

publicVar and publicFunction are public members, accessible from outside the class.

privateVar and privateFunction are private members, only accessible within the class.

protectedVar and protectedFunction are protected members, accessible within the class and its derived classes.

In C++, visibility modes(also called access specifiers) control how class members (variables(int a,b,c) and functions (void fun())) can be accessed from outside the class or by derived classes.1

In C++, visibility modes (Access Specifiers) control how class members (variables and functions) can be accessed from outside or inside a class. They are mainly used in Object-Oriented Programming (OOP) to protect data and implement encapsulation.

C++ has three visibility modes:

  1. Public
  2. Private
  3. Protected

1. Public Visibility Mode

If a member is declared public, it can be accessed from anywhere(inside or outside(::)) in the program.

✔ Accessible:

  • Inside the class
  • Outside the class
  • In derived (child) classes

Syntax

class ClassName {
public:
int a;
void display();
};

#include <iostream.h>


class Student {
public:
int age;

void show() {
cout << “Age: ” << age;
}
};

int main() {
Student s;
s.age = 20;
s.show();
}

Here age and show() are public, so they can be accessed outside the class.

2. Private Visibility Mode

If a member is private, it can be accessed only inside the class.

❌ Not accessible:

  • Outside the class
  • In derived classes directly

✔ Accessible:

  • Inside the same class

Syntax

class ClassName {
private:
int a;
};

#include <iostream.h>

class Student {

private:

    int agee;

 

public:

    void setAgee(int a) {

        agee = a;

    }

 

    void show() {

        cout << “Ageee: ” << agee;

    }

};

 

int main() {

    Student ss;

    ss.setAgee(20);

    ss.show();

}

agee is private, so we use public functions to access it.

3. Protected Visibility Mode

If a member is protected, it can be accessed:

✔ Inside the class
✔ In derived (child) classes(parent and child relation)
❌ Not accessible outside(::) the class

Syntax

class ClassName

{
protected:
int a;
};

#include <iostream.h>


class Parentt {
protected:
    int xx;
};

class Childd : public Parentt {
public:
    void setValuee() {
        xx = 10;
    }

    void shoow() {
        cout << xx;
    }
};

int main() {
    Childd ch;
    ch.setValuee();
    ch.shoow();
}

xx is protected, so it can be used in the child class.

Comparison Table

Visibility Mode

Same Class

Derived Class

Outside Class

Public

✔ Yes

✔ Yes

✔ Yes

Private

✔ Yes

❌ No

❌ No

Protected

✔ Yes

✔ Yes

❌ No

 

 

 

Default visibility

  • In class → private
  • In struct → public

Example:

class Test {
    int a;   // private by default
};

Real-Life Example

Think of a Bank Account:

  • Public → Account holder name
  • Private → Password / PIN
  • Protected → Data accessible by bank system or branch manager

This helps in data security and controlled access.

 

 

Inheritance in C++