# 13.2.1: Classes and Objects - Data Members

$$\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}}$$

### 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.

1. Public
2. Private
3. 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:

1. 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

// A method that belongs to this class
double compute_area()
{
}
};

// main function
int main()
{
// We declare a new instance of the class Circle
Circle newObj;

// accessing public data member outside class

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

1. 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:

// public member function
public:
// member function can access private
double compute_area()
{
}
};

// 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

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
^
31:9: error: within this context
^ 

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:

// public member function
public:
void compute_area(double r)
{   // member function CAN access private

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
1. 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