Skip to main content
Engineering LibreTexts

1.9: Accessing Data Members

  • Page ID
  • The public data members are also accessed in the same way given however the private data members are not allowed to be accessed directly by the object. Accessing a data member depends solely on the access control of that data member.
    This access control is given by Access modifiers in C++. There are three access modifiers : public, private and protected.

    // C++ program to demonstrate  
    // accessing of data members  
    #include <bits/stdc++.h> 
    using namespace std; 
    class Geeks {     
       // Access specifier     
          // Data Members        
          string geekname;        
          // Member Functions()        
          void printname()        
             cout << "Geekname is: " << geekname;
    int main()
         // Declare an object of class geeks
         Geeks obj1;
         // accessing data member
         obj1.geekname = "Steve Jobs";
         // accessing member function
         return 0; 


    Geekname is: Abhi

    Adapted from:
    "C++ Classes and Objects" by Abhirav Kariya, Geeks for Geeks is licensed under CC BY 4.0

    • Was this article helpful?