What is visibility mode in c++





In C++, the visibility mode typically refers to the access level of class members within a class hierarchy. There are three visibility modes in C++: public, protected, and private.

  1. Public: Members declared as public are accessible from outside the class through object instances. These members can be accessed by any function or class.
  2. Protected: Members declared as protected are accessible from within the class itself and by derived classes. However, they are not accessible from outside the class hierarchy.
  3. Private: Members declared as private are accessible only from within the class itself. They cannot be accessed from outside the class, not even by derived classes


Public Visibility Mode in C++ Class


In C++, public visibility mode is used to specify that the members (attributes and methods) of a class can be accessed by other classes and functions outside the class. Public members are accessible anywhere in the program.



Syntex For Public Visibility Mode in C++ Class:

    
      class MyClass {
      public:
        int publicVariable;
        void publicMethod();
      };
    
  


Example For Public Visibility Mode in C++ Class:

#include <iostream>
using namespace std;

class A1 {
public:
    int x;  // Public member variable
};

int main() {
    A1 obj;  // Creating an object of class A1

    // In this scenario, we're directly accessing and modifying the public member variable 'x'.
    // There is not data hiding, as the member is already publicly accessible.
    obj.x = 5;  // Modifying the value of 'x'
    cout << obj.x;  // Output the modified value of 'x'

    return 0;
}
5


Explaination:
  1. We have a class A1 with a public member variable x.
  2. We create an object obj of class A1.
  3. We directly modify the value of x using obj.x = 5;.
  4. Finally, we output the modified value of x.


Private Visibility Modes in C++ Class

Private visibility mode is used to specify that the members of a class can only be accessed by other members of the same class. Private members are not accessible outside the class, including other classes and functions.



Syntex For Private Visibility Mode in C++ Class

    
      class MyClass {
      private:
        int privateVariable;
        void privateMethod();
      };
    
  


Example For Private Visibility Mode in C++ Class

#include <iostream>
using namespace std;

class A1 {
private:
    int x;  // Private member variable
};

int main() {
    A1 obj;  // Creating an object of class A1

    // Attempting to directly access or modify 'x' will result in a compilation error because it's private.
    obj.x = 5;  // Error: 'int A1::x' is private within this context
    cout << obj.x;  // Error: 'int A1::x' is private within this context

    return 0;
}
Output:
Program will not execute.


Example For Private Visibility Mode in C++ Class using function.

#include <iostream>
using namespace std;

class A1 {
private:
    int x;  // Private member variable
public:
    // Method to set the value of x
    void setX(int value) {
        x = value;
    }

    // Method to get the value of x
    int getX() {
        return x;
    }
};

int main() {
    A1 obj;  // Creating an object of class A1

    // Accessing the private member variable 'x' using a public member function
    obj.setX(5);  // Setting the value of 'x'
    cout << "Modified value of 'x': " << obj.getX() << endl;  // Output the modified value of 'x'

    return 0;
}
Output:
Modified value of 'x': 5


Protected Visibility Modes in C++ Class

In C++, protected visibility mode is used to specify that the members of a class can be accessed by other members of the same class and its derived classes. Protected members are not accessible outside the class hierarchy.


Syntex For protected Visibility Mode in C++ Class

    
      class BaseClass {
      protected:
        int protectedVariable;
        void protectedMethod();
      };

      class DerivedClass : public BaseClass {
        // Derived class can access the protected members of the base class.
      };
    
  


Example For Problem In protected Visibility Mode in C++ Class

#include <iostream>
using namespace std;

class A1 {
protected:
    int x = 6; // Declare x as protected
};

int main() {
    A1 obj; // Object is ready
    // obj.x = 5; // Error: 'int A1::x' is protected within this context
    cout << obj.x; // Error: 'int A1::x' is protected within this context
    return 0;
}
Output:
Program will not execute.


Example For protected Visibility Mode in C++ Class

#include <iostream>
using namespace std;

class A1 {
protected: // Now 'x' is protected, accessible within derived classes
    int x; // 'x' is accessible within this class and its derived classes
};

class Derived : public A1 {
public:
    void modifyX() {
        x = 5; // Accessing 'x' from derived class
    }

    void displayX() {
        cout << "Value of x from Derived class: " << x << endl;
    }
};

int main() {
    Derived obj; // Creating object of Derived class
    obj.modifyX(); // Modifying 'x' from the derived class
    obj.displayX(); // Displaying the modified value of 'x'

    return 0;
}