In C++, visibility modes 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:
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();
}
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.