Advertisement



< Prev
Next >



C++ Class Visibilty Labels






The components of a class such as its data members or functions are called members of a class i.e. class members. A class member is declared with an visibility labels, that specifies how it will be accessed outside its class. A C++ class member can take any of the visibility labels, such as - public, protected and private.




Public Visibility Label


A class member, be it an data variable or a function can be declared with the public keyword. A class member declared with a public visibility label is accessible and visible outside its class by using the object of its class. Let's see example-


A.cpp
#include<iostream>

using namespace std;

class A
{
public:
int a = 10;
void message(); 	//declaration of the function named message()
};


//Defition of the function message of class A
void A :: message()
{
cout<< "Hello from the class A";
}



int main()
{
A ob;		//Creating an object of class A

//Accessing the data member a, associated with the object of class A
cout<< "The value of a : " << ob.a << "\n";

//Calling the function message of class A, using its object
ob.message();
}



Output is :


The value of a : 10
Hello from the class A


Program Analysis




Note :


Class A has declared the function message() and data member a with a public visibility mode and that's why it is visible and accessible outside class A(in the main() function), using its object.


Advertisement




  • Private Visibility Label


  • A class member - data member or function declared with the private keyword will be hidden and inaccessible outside its class and it won't serve any purpose. Private member of a class is only accessible in the class in which it is declared or defined and not outside it. Let's see an example -

    A.cpp
    #include<iostream>
    
    using namespace std;
    
    class A
    {
    public:
    int a = 10;
    void message(); 	//declaration of the function named message()
    };
    
    
    //Defition of the function message of class A
    void A :: message()
    {
    cout<< "Hello from the class A";
    }
    
    
    
    int main()
    {
    A ob;		//Creating an object of class A
    
    //Accessing the data member a, associated with the object of class A
    cout<< "The value of a : " << ob.a << "\n";
    
    //Calling the function message of class A, using its object
    ob.message();
    }
    
    B.java
    class B
    {
    public static void main(String... ar)
    {
    A ob= new A();
    System.out.println(ob.a);
    }
    }
    


    Output is -


    A.cpp: In function 'int main()':
    A.cpp:26:34: error: 'int A::a' is private within this context
     cout<< "The value of a : " << ob.a << "\n";
                                      ^
    A.cpp:8:9: note: declared private here
     int a = 10;
             ^~
    A.cpp:29:12: error: 'void A::message()' is private within this context
     ob.message();
                ^
    A.cpp:14:6: note: declared private here
     void A :: message()


    Program Analysis


    Compiler shows a compile error which clearly states that data member named a of class A and the function message has a private access, hence it can't be used outside class A(in which it is declared and defined.)




  • Protected Visibility Label


  • A C++ class member such as - data member or function declared with the protected keyword will be accessible and visible to -



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement