@@ -97,18 +97,86 @@ void getCircleFromShape(Shape* shape)
9797
9898ELEMENT_CODE (InheritanceUsageExample) 
9999{
100-  std::vector<Shape*> shapes {
101-  new  Circle (5 ),
102-  new  Rectangle (3 , 5 ),
103-  new  Triangle (12 , 3 , 30 )
104-  };
100+  std::vector<std::unique_ptr<Shape>> shapes;
101+  shapes.emplace_back (new  Circle (5 ));
102+  shapes.emplace_back (new  Rectangle (3 , 5 ));
103+  shapes.emplace_back (new  Triangle (12 , 3 , 30 ));
105104
106105 auto  total_area = std::reduce (begin (shapes),
107106 end (shapes),
108107 0 .f ,
109-  [](float  area, Shape*  shape) {
108+  [](float  area, auto &  shape) {
110109 return  area + shape->getArea ();
111110 });
112111 std::cout << " total area of all shapes are " " \n " 
112+ }
113+ 
114+ 
115+ //  inheritance access control
116+ 
117+ class  base 
118+ {
119+ public: 
120+  base () {
121+  static  auto  print_once = [&] {
122+  std::cout << " class can access all of its members " 
123+  " no matter access control\n " 
124+  std::cout << " private var: " " \n " 
125+  std::cout << " protected var: " " \n " 
126+  std::cout << " public var: " " \n " 
127+  return  true ;
128+  } ();
129+  }
130+ public: 
131+  int  public_var{ 1  };
132+ protected: 
133+  int  protected_var{ 2  };
134+ private: 
135+  int  private_var{ 3  };
136+ };
137+ 
138+ class  public_derived  
139+  : public base { };
140+ 
141+ class  protected_derived  
142+  : protected base { };
143+ 
144+ class  private_derived 
145+  : private base { 
146+ public: 
147+  private_derived () {
148+  std::cout << " derived classes can access protected and " 
149+  " public base variables inside of class\n " 
150+  //  std::cout << "private var: " << private_var << "\n";
151+  std::cout << " base protected var: " " \n " 
152+  std::cout << " base public var: " " \n " 
153+  }
154+ };
155+ 
156+ ELEMENT_CODE (InheritanceAccessControl)
157+ {
158+  base b;
159+  public_derived pd;
160+  protected_derived pro_d;
161+  private_derived priv_d;
162+  std::cout << " access to base class variables from " 
163+  " derived class instances depends on inheritance type\n " 
164+ 
165+  std::cout << " when an inheritance marked as protected; all public " 
166+  " members of base goes to protected inside of derived class\n " 
167+ 
168+  std::cout << " when an inheritance made as private then members of base " 
169+  " goes to private inside of derived class\n " 
170+ // pd.public_var;
171+ // pd.protected_var;
172+ // pd.private_var;
173+ // 
174+ // pro_d.public_var;
175+ // pro_d.protected_var;
176+ // pro_d.private_var;
177+ //  
178+ // priv_d.public_var;
179+ // priv_d.protected_var;
180+ // priv_d.private_var;
113181
114182}
0 commit comments