Computer Science, asked by rherzog, 9 months ago

Write the differences between protected and private members in a class. Write the benefits of using a generic function.

Attachments:

Answers

Answered by SatwatAgnihotri
0

Explanation:

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 Parent {

// protected data members

protected:

int id_protected;

};

// sub class or derived class

class Child : public Parent {

public:

void setId(int id)

{

// Child class is able to access the inherited

// protected data members of the base class

id_protected = id;

}

void displayId()

{

cout << "id_protected is: "

<< id_protected << endl;

}

};

// main function

int main()

{

Child obj1;

// member function of the derived class can

// access the protected data members of the base class

obj1.setId(81);

obj1.displayId();

return 0;

}

please Mark me as a brainliest because I have given answer only for you please please please

Similar questions