What is Private Access Specifier in C++


The access specifier is a defining code element that can determine which elements of a program are allowed to access a specific Member variable and member function. The private Access Specifier is Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.

The program defines a class named student, which has two private data members name and age, and two public member functions getData() and display(). The getData() function prompts the user to enter the student's name and age through standard input using cin and stores them in the private data members name and age, respectively. The display() function outputs the values of name and age to the standard output using cout.

In the main() function, an object of the student class is created using the default constructor. Then, the getData() member function of the student class is called on the object to prompt the user to enter the student's name and age. Since the name and age data members are private, they cannot be accessed or modified directly from outside the student class. Instead, the getData() function is used to encapsulate the data input process and store the values in the private data members.

Finally, the display() member function of the student class is called on the object to output the entered values of name and age to the standard output using cout. Since the display() function is public, it can be called from outside the student class to display the values of name and age without exposing the private data members directly.

The private access specifier restricts the access to the name and age data members to only the member functions of the student class. This means that any other function or object in the program cannot access or modify the name and age data members directly, but must use the public member functions to encapsulate the data and manipulate it. Overall, this program provides an example of using a class definition in C++ with private access specifier to encapsulate and protect data related to a student object.

Source Code

#include<iostream>
using namespace std;
/*
Access Specifier
    Public
    Private
    Protected
*/
//What is Private Access Specifier in C++
class student
{
private:
    string name;
    int age;
 
public:
    void getData()
    {
        cout<<"\nEnter Name & Age :"<<endl;
        cin>>name;
        cin>>age;
    }
    void display()
    {
        cout<<"Name :"<<name<<endl;
        cout<<"Age  :"<<age<<endl;
    }
};
int main()
{
    student o;
    o.getData();
    o.display();
 
    return 0;
}
 
 

Output

Enter Name & Age :
Tutor
27
Name :Tutor
Age  :27
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples