Home » C++ programming language

Difference between Private and Protected in C++ with Example

Private vs Protected in C++: In this tutorial, we are going to learn about the private members and protected members in C++, what are the differences between private and protected members in C++?
Submitted by IncludeHelp, on November 26, 2019

In the concept of C++ class objects, there are three access modifiers: 1) private, 2) protected and 3) public. They are used based on their properties. Here, we will understand what are private and protected members and what are the differences between them?

1) Private members

Private members are declared with the keyword private followed by the colon (:) character in the class and they are accessible within the class only in which they are declared. Private members cannot be accessed outside of the class.

Private members can be data members (variables) and member functions (functions) and both can be accessed within the same class, they can be used within the private/public member functions of the same class.

Consider the below example, it contains two private members name and city and they are being used within the public member functions getPerson() and dispPerson().

// example of private members

#include <iostream>
using namespace std;

class person {
    // private memebers
private:
    string name;
    int age;

    // public memebers
public:
    void getPerson()
    {
        cout << "Enter name: ";
        cin >> name;
        cout << "Enter age: ";
        cin >> age;
    }
    void dispPerson()
    {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

// main function
int main()
{
    //creating object
    person per;
    per.getPerson();
    per.dispPerson();

    return 0;
}

Output

Enter name: Shivang
Enter age: 21
Name: Shivang
Age: 21

2) Protected members

Protected members are declared with the keyword protected followed by the colon (:) character in the class and they are accessible within the class in which they are declared and also accessible in the derived or subclass. Protected members are used in the concept of inheritance. Just like Private Members Protected members cannot be accessed outside of the class (except in the derived class).

Protected members can be data members (variables) and member functions (functions) and both can be accessed within the same class and derived or subclass or child class.

Consider the below example, there are two classes person which is a base class and person2 which a derived class, person class contains two private members name and city, one protected member roll_no and private members are being used within the public member functions getPerson() and dispPerson().

In the person1 class, there is one private member city and three public member functions set_roll_no() – which is using to set the value of roll_no that is a protected member in person class, getPerson1() – which is calling the getPerson() member function of person class and also getting the input of city. Similarly, dispPerson1() – which is calling the dispPerson() member function and also printing the value of roll_no and city.

// example of protected members

#include <iostream>
using namespace std;

// base class
class person {
    //private members
private:
    string name;
    int age;

    // protected member
protected:
    int roll_no;

    //public members
public:
    void getPerson()
    {
        cout << "Enter name: ";
        cin >> name;
        cout << "Enter age: ";
        cin >> age;
    }
    void dispPerson()
    {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

// derived class
class person1 : public person {
    // private members
private:
    string city;

    // public members
public:
    void set_roll_no(int r)
    {
        // here roll_no is the protected member of person
        // class, it is accessible here
        roll_no = r;
    }
    void getPerson1()
    {
        // calling getPerson() to read basic details
        getPerson();
        // input city
        cout << "Enter city: ";
        cin >> city;
    }
    void dispPerson1()
    {
        // displaying roll_no
        cout << "Roll No.: " << roll_no << endl;
        // calling dispPerson() to print basic details
        dispPerson();
        // displaying city also
        cout << "City: " << city << endl;
    }
};

// main function
int main()
{
    //creating object
    person1 per;
    // setting roll number
    per.set_roll_no(101);
    // getting all details
    per.getPerson1();
    // printing all details
    per.dispPerson1();

    return 0;
}

Output

Enter name: Shivang
Enter age: 21
Enter city: Indore
Roll No.: 101
Name: Shivang
Age: 21
City: Indore

Difference between private and protected members

Private members Protected members
Private members are declared with the keyword private followed by a colon (:) character. Protected members are declared with the keyword protected followed by a colon (:) character.
Private members are accessible within the same class in which they are declared. Protected members are accessible within the same class and within the derived/sub/child class.
Private members can also be accessed through the friend function. Protected members cannot be accessed through the friend function.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.