13.2.1: Classes and Objects - Data Members
- Page ID
- 29113
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Accessing Data Members
There are 3 types of access modifiers available in C++:A Class is a user defined data-type which has data members and member functions.
- Public
- Private
- Protected
Note: If we do not specify any access modifiers for the members inside the class then by default the access modifier for the members will be Private.
Let us now look at each one these access modifiers in details:
- Public: All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.
Example:
// C++ program to demonstrate public // access modifier #include<iostream> using namespace std; // class definition class Circle { public: // a datamember that belongs to the class Circle double radius; // A method that belongs to this class double compute_area() { return 3.14*radius*radius; } }; // main function int main() { // We declare a new instance of the class Circle Circle newObj; // accessing public data member outside class newObj.radius = 5.5; cout << "Radius is: " << newObj.radius << "\n"; cout << "Area is: " << newObj.compute_area(); return 0; }
Several things in this code. We declare a class named Circle. Classes have data members, and functions (we will talk more about classes as we go along).
A data member is simply a variable that belongs to this class. The method is similar to a function, bu tit belongs to this class that it is defined in - in this case the Circle class.
Because the data member and the method are defined as public, we can access them both from the main() function. Notice that to access them we must prepend instance name followed by a period in front of the name: newObj.radius and newObj.compute_area().
Radius is: 5.5 Area is: 94.985
-
Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.
Example:
// C++ program to demonstrate private // access modifier #include<iostream> using namespace std; class Circle { // private data member private: double radius; // public member function public: // member function can access private // data member radius double compute_area() { return 3.14*radius*radius; } }; // main function int main() { // creating object of the class Circle obj; // trying to access private data member // directly outside the class // THIS IS AN ERROR obj.radius = 1.5; cout << "Area is:" << obj.compute_area(); return 0; }
The output of above program will be a compile error. In main() we attempt to access the obj.radius dta member outside of the class - but since this data member is now declared private we are not allowed to access it from outside of the class it is defined in, giving us the error:
In function 'int main()': 11:16: error: 'double Circle::radius' is private double radius; ^ 31:9: error: within this context obj.radius = 1.5; ^
However, we can access the private data members of a class indirectly bu using the public member functions of the class.So we alter the code to pass the argument to the public method computer_area() and the method can set the value of the private member radius, as well as calculate the area, and output the results.
// C++ program to demonstrate private // access modifier #include<iostream> using namespace std; class Circle { // private data member private: double radius; // public member function public: void compute_area(double r) { // member function CAN access private // data member radius radius = r; double area = 3.14*radius*radius; cout << "Radius is: " << radius << endl; cout << "Area is: " << area; } }; // main function int main() { // creating object of the class Circle obj; // Pass the argument to the method who CAN // access the private member from within the class obj.compute_area(1.5); return 0; }
Radius is: 1.5 Area is: 7.065
-
Protected: Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.
Example:
// C++ program to demonstrate // protected access modifier #include <bits/stdc++.h> using namespace std; // base class class myParent { // protected data members protected: int id_protected; }; // sub class or derived class class thisChild : public myParent { public: void setId(int id) { // Child class is able to access the inherited // protected data members of base class id_protected = id; } void displayId() { cout << "id_protected is: " << id_protected << endl; } }; // main function int main() { thisChild newObj; // member function of the derived class can // access the protected data members of the base class newObj.setId(81); newObj.displayId(); return 0; }
Output:
id_protected is: 81
Adapted from:
"Access Modifiers in C++" by Abhirav Kariya and Harsh Agarwal , Geeks for Geeks
"C++ Classes and Objects" by BabisSarantoglou, Geeks for Geeks is licensed under CC BY-SA 4.0