1.9: Accessing Data Members
( \newcommand{\kernel}{\mathrm{null}\,}\)
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 public: // 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 obj1.printname(); return 0; }
Output:
Geekname is: Abhi
Adapted from:
"C++ Classes and Objects" by Abhirav Kariya, Geeks for Geeks is licensed under CC BY 4.0