Accessing Public and Private Data Members in C++
Accessing Public and Private Data Members in C++
In this article, you will learn about accessing public and private and protected data members in C++.
As discussed in the tutorial “Access Controls in Classes”, there are three kinds of access specifies being used in C++.
- public
- private
- protected
Here we can say that the data members declared in a class may be public, private or protected. We need to write different code to access these different kinds of data members. Data members declared as public can be accessed directly anywhere in the program but private and protected variables cannot be accessed directly.
Private data members are used when we want to enforce encapsulation while protected data members are used in case of inheritance. Encapsulation and inheritance are two main concepts of object oriented programming. Private and protected data members will be discussed in detail in the respective tutorials related to encapsulation and inheritance.
Accessing Public Data Members
The following code shows that how public data members can be declared in a class and accessed in the main function.
In line 8, we can see that the data members declared in class Person are public. In line 15, an object of class Person is declared having name ‘p’. This object can access all the public data members declared in class Person as shown in lines 16, 17, 19 and 20. It is clear from the above code that to access the public data members of a class, we need to use an object of the class followed by a dot (.) operator.
Accessing Private Data Members
Data members declared as private in a class cannot be accessed directly as public data members. As discussed earlier private data members will be discussed in detail while discussing encapsulation but at the moment you should know that we need to define a public function through which the private data members of class can be accessed by the objects of the class as shown in the code below.
You can see that the data members of class Person are declared as private as shown in line 8. Due to the private access specifier, these data members cannot be accessed directly by the object of the class in main function. In order to allow the objects of the class to access these data members, we need to define methods in the class through which the private data members can be accessed by the objects.
To do so, three public functions are defined in the class having names setName, setPhone_number and speak on lines 12, 16 and 20 respectively. These functions are used by the object of the class in main function as shown on lines 30, 31 and 32. The same code will be explained in more detail while discussing encapsulation in a later tutorial.
Accessing Protected Data Members
In order to access protected data members, we need to define parent and child classes which will be discussed in the tutorial related to ‘Inheritance’.
More Related Articles For You
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- Data Types in C++
- C++ Variables
- Types of operators in C++
- Decision making in C++
- C++ Loop Types
- Storage Classes in C++
- Functions in C++
- Classes and Objects in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Constructors and Destructors in C++
- Static Keywords in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Types of Inheritance
- Order of Constructor Call in Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Abstract Class and Pure Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++
C++ Tutorial
C++ Quizzes