Created
September 3, 2018 08:03
-
-
Save dibakarsutradhar/bdeee1cc2b0174a0ba84e9ef797e4e31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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