Skip to content

Instantly share code, notes, and snippets.

@dibakarsutradhar
Created September 3, 2018 08:03
Show Gist options
  • Save dibakarsutradhar/bdeee1cc2b0174a0ba84e9ef797e4e31 to your computer and use it in GitHub Desktop.
Save dibakarsutradhar/bdeee1cc2b0174a0ba84e9ef797e4e31 to your computer and use it in GitHub Desktop.
// c++ oop protected access modifier
#include <iostream>
using namespace std;
// defining a class
class Parent {
protected: // we defined that this class data members will be protected
int number_protected;
};
// defining a sub class or derived class
class Child : public Parent {
public: // and we kept the member function public
void setNumber(int number) {
number_protected = number; // Child class can access the inherited protected data members of Parent Class
}
void displayID() {
cout << "Protected Number is: " << number_protected << endl;
}
};
//main function
int main(){
Child obj1; // create a child object
obj1.setNumber(2); // member function of derived class can access the protected data members of base class
obj1.displayID();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment