Theory
કોઈ પણ ખાનું ખોલી શકતું હતું
CanteenTill class ની પહેલી આવૃત્તિ દિવસની રોકડ સાદા variable માં રાખતી હતી. કોઈ પણ code, ગમે ત્યાંથી, લખી શકતો હતો:
till.cash = 0;
એક intern ના ખામીવાળા loop એ સાંજે 4 વાગ્યે બરાબર એ જ કર્યું. કોઈ ભૂલ નહીં, કોઈ ચેતવણી નહીં, 18,000 રૂપિયાની નોંધ ગઈ.
ઉકેલ "સાવધ રહો" નથી. ઉકેલ એ ભાષાનું એવું લક્ષણ છે જે compiler ને આવી લીટીની ના પાડવા મજબૂર કરે. C++ તમને ત્રણ તાળાં આપે છે: public, private, protected.
Theory
Counter, રસોડું, રોકડનું ખાનું
Canteen માં પગ મૂકો:
- Counter એ public છે: કોઈ પણ ત્યાં ઊભા રહીને order આપી શકે.
- રસોડું એ protected છે: કર્મચારીઓ અને કુટુંબની franchise શાખાઓ અંદર જઈ શકે, ગ્રાહકો નહીં.
- રોકડનું ખાનું એ private છે: ફક્ત માલિક જ ખોલે, કર્મચારીઓ સુધ્ધાં બહાર રહે.
Class નો દરેક member આ ત્રણમાંના એક ક્ષેત્રમાં વસે છે, અને compiler એ એવો ચોકીદાર છે જે ક્યારેય ઊંઘતો નથી.
Theory
ત્રણેય specifiers, ઔપચારિક રીતે
Access specifier નક્કી કરે છે કે એની પછી declare થયેલા members ને કોણ વાપરી શકે:
- public: જ્યાં જ્યાં object દેખાય ત્યાંથી,
main()સહિત, પ્રાપ્ય. - private: ફક્ત class નાં પોતાનાં member functions ની અંદરથી પ્રાપ્ય. બહાર કોઈ નહીં, derived class પણ નહીં.
- protected: બહારના જગત માટે private જેવું, પણ derived classes એને વાપરી શકે છે (આ inheritance માં મહત્ત્વનું છે, બે પાઠ પછી).
Specifier એની પછીના દરેક member પર લાગુ પડે છે, જ્યાં સુધી પછીનું specifier ન આવે.
Practical
ખાનું, બરાબર તાળાબંધ
#include <iostream>
using namespace std;
class CanteenTill {
private:
double cash; // the drawer: owner only
protected:
double costPrice; // the kitchen: this class + child classes
public:
int ordersToday; // the counter: anyone
void addSale(double amount) { // public gate to the private drawer
cash = cash + amount;
}
double report() { return cash; }
};
int main() {
CanteenTill till;
till.ordersToday = 1; // ok: public
till.addSale(30.0); // ok: public function touches private cash
// till.cash = 0; // ERROR: 'cash' is private
return 0;
}
At a glance
કોણ શું વાપરી શકે
| ક્યાંથી વપરાય | public | protected | private |
|---|---|---|---|
| એ જ class નાં functions | હા | હા | હા |
| Derived class નાં functions | હા | હા | ના |
| બહારથી (main, બીજો code) | હા | ના | ના |
Think first
Compiler ને ધારો
ઉપરનો class વાપરીને, main() ચલાવે છે:
cout << till.cash;
Tap કરતાં પહેલાં: compile ની ભૂલ કે output? અને જો ભૂલ હોય, તો cash ને private રાખતો એક લીટીનો ઉકેલ શું છે?
Show the answer
Compile ની ભૂલ: 'cash' is private within this context જેવું કંઈક. ઉકેલ એ છે કે class પહેલેથી આપે છે એ public દરવાજેથી જવું: cout << till.report();. Encapsulation ની આખી ભાત આ જ છે: private data, અને પહોંચની મધ્યસ્થી કરતાં public functions. ખાનું બંધ જ રહે છે; counter એના વિશેના પ્રશ્નોના જવાબ આપે છે.
Quiz
`class CanteenTill { double cash; ... }` ની ટોચે એક member કોઈ specifier **વગર** declare થયો છે. cash ને કોણ વાપરી શકે?
- ફક્ત class નાં પોતાનાં member functions: class ના members મૂળભૂત રીતે private હોય છે
- બધા: members મૂળભૂત રીતે public હોય છે
- Class અને એનાં સંતાનો: members મૂળભૂત રીતે protected હોય છે
- Specifier લખાય નહીં ત્યાં સુધી કોઈ જ નહીં
Show the answer
ફક્ત class નાં પોતાનાં member functions: class ના members મૂળભૂત રીતે private હોય છે
class માં, પહેલા specifier પહેલાંનું બધું private હોય છે. જાણીતી જોડિયા હકીકત: struct માં મૂળભૂત public હોય છે. class સામે struct નો એ મૂળભૂત ભેદ પ્રિય 2 marks નો પ્રશ્ન છે. વિકલ્પ B એ struct નું વર્તન વર્ણવે છે, વિકલ્પ C નું મૂળભૂત C++ માં અસ્તિત્વમાં જ નથી, અને વિકલ્પ D કાલ્પનિક છે: members ને હંમેશા કોઈ ને કોઈ પહોંચનું સ્તર મળે જ છે.
Watch out
મૂળભૂતનો ફાંદો
પરીક્ષાઓને ગમે છે: "C++ માં class અને struct વચ્ચે શું ભેદ છે?" અપેક્ષિત જવાબ બરાબર એક લીટીનો છે: class ના members મૂળભૂત રીતે private હોય છે, struct ના members મૂળભૂત રીતે public. એમના વિશે બાકી બધું સરખું છે.
બીજો ફાંદો: inheritance આવે ત્યાં સુધી protected નકામું લાગે છે. જવાબમાં એને "private જેવું જ" ન કહો: derived class માટે એ જુદી રીતે વર્તે છે, અને એ ભેદ જ મુદ્દો છે.
Theory
આ encapsulation નું બળ છે
હવે પછીના પાઠો આ ખ્યાલને નામ આપે છે: data hiding અને encapsulation. નોંધો કે એ ફક્ત એટલા માટે શક્ય છે કારણ કે આ specifiers અસ્તિત્વમાં છે: private વગર "છુપાવવું" એ compiler થી અમલમાં મુકાયેલો નિયમ નહીં પણ એક વિનમ્ર વિનંતી હોત. પછી નોકરીના interview માં તમે getters અને setters લખશો, ત્યારે તમે બરાબર આ જ કોષ્ટક લાગુ પાડતા હશો.
Summary
Key takeaways
- public: ગમે ત્યાંથી પ્રાપ્ય; private: ફક્ત class નાં પોતાનાં functions; protected: class વત્તા derived classes.
- Specifier એની પછીના બધા members પર લાગુ પડે છે, જ્યાં સુધી પછીનું ન આવે.
- Class નું મૂળભૂત private છે; struct નું મૂળભૂત public છે (જાણીતો 2 marks નો પ્રશ્ન).
- બહારનો code private data ને ફક્ત public member functions (getters/setters) દ્વારા જ અડે છે.
- protected એની કિંમત inheritance માં વસૂલ કરે છે: સંતાનો એને વાપરી શકે, બહારના નહીં.
- Memory hook: counter, રસોડું, રોકડનું ખાનું.