Advertisement

C++ protected member function - code with abuzar

 In this tutorial i will tell about the protected member in C++


https://abuzartec.blogspot.com/2021/04/c-protected-member-function-code-with.html


Protected Member in C++

When preceding the name of a base class , the protected keyword specifies that the public and protected member of the base class are protected members of its derived classes .

Protected member are not as private member member which are accessible to only to member of the class in which they are declared , but they are not as a public member , which are accessible by any function .

Protected member are also declared at static are accessible to any friend or member function of a derived class .


Example 

#include <iostream>
using namespace std;
 
class Box {
   protected:
      double width;
};
 // SmallBox is the derived class.
class SmallBox:Box { 
   public:
  void setSmallWidthdouble wid );
  double getSmallWidthvoid );
};
 
// Member functions of child class
double SmallBox::getSmallWidth(void)
{
  return width ;
}
 
void SmallBox::setSmallWidth(double wid)
{
   width = wid;
}
 
// Main function for the program
int main() {
   SmallBox box;
 
// set box width using member function
 box.setSmallWidth(5.0);
 cout <<"Width of box :"
<<box.getSmallWidth()<<endl;
 
   return 0;
}

Code is compiled and executed than the result is .

Width of box : 5


Please! Write comment if you find anything incorrect .

This article is contributed by Mohd Abuzar if  you like code with abuzar and would like to contribute you can also write an article or mail at abuzar.abuzar.mohd325@gmail.com .





Post a Comment

0 Comments