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.
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.
class MyClass {
public:
int publicVariable;
void publicMethod();
};
#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
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.
class MyClass {
private:
int privateVariable;
void privateMethod();
};
#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; }
Program will not execute.
#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; }
Modified value of 'x': 5
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.
class BaseClass {
protected:
int protectedVariable;
void protectedMethod();
};
class DerivedClass : public BaseClass {
// Derived class can access the protected members of the base 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; }
Program will not execute.
#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; }