גישה לשינויי גישה ב-C++

גישה לשינויי גישה ב-C++

משנה גישה הם מילות מפתח מיוחדות ב-C++ השולטים בנראות של חברי הכיתה (נתונים ופונקציות).
הם עוזרים ביישום הסתרת נתונים על ידי הגבלת או מתן גישה לחלקים מסוימים של מחלקה.

סוג_של_גישה_משנה

בואו נדון בזה אחד אחד

1. מפרט ציבורי

מפרט ציבורי הופך את חברי המחלקה (משתנים ופונקציות) לנגישים מכל מקום - בתוך המחלקה מחוץ למחלקה או אפילו קבצים אחרים.

2. מפרט פרטי

Private specifier הופך את חברי המחלקה לנגישים רק בתוך המחלקה עצמה (בעיקר מתודות חבר) ומשמש בעיקר להסתרת נתונים מבחוץ כדי שנוכל לבצע שינויים פנימיים מאוחר יותר מבלי לשנות את הקוד של המשתמשים בה. כברירת מחדל, כל חברי הכיתה ב-C++ הם פרטיים אם לא מוזכר מפרט.

CPP
   #include          using     namespace     std  ;   class     Employee     {   private  :         // salary and empId cannot be accessed       // from outside the Class      double     salary  ;      int     empID  ;   public  :         string     name  ;     // Name can be accessed anywhere          Employee  (  string     n       double     s       int     id  )     {      name     =     n  ;      salary     =     s  ;      empID     =     id  ;      }   };   int     main  ()     {      Employee     emp  (  'Fedrick'       50000       101  );      cout      < <     'Name: '      < <     emp  .  name      < <     endl  ;      return     0  ;   }   

תְפוּקָה
Name: Fedrick  

מה קורה אם ננסה לגשת לחברים פרטיים?

הקוד נותן שגיאת מהדר כפי שאנו יכולים לראות להלן.

C++
   #include          using     namespace     std  ;   class     Employee     {   private  :         double     salary  ;      int     empID  ;   public  :         string     name  ;      // Constructor      Employee  (  string     n       double     s       int     id  )     {      name     =     n  ;      salary     =     s  ;      empID     =     id  ;      }   };   int     main  ()     {      Employee     emp  (  'Fedrick'       50000       101  );      cout      < <     emp  .  salary      < <     endl  ;         return     0  ;   }   

תְפוּקָה

 compilation error    
prog.cpp: In function ‘int main()’:
prog.cpp:22:17: error: ‘double Employee::salary’ is private within this context
cout < < emp.salary < < endl;
^~~~~~
prog.cpp:6:12: note: declared private here
double salary;
^~~~~~

3. מפרט מוגן

  • המפרט המוגן מנגיש חברים בתוך המחלקה עצמה ובתוכו נִגזָר כיתות (ילדים) אך לא יוצרים קוד חיצוני.
  • הוא משמש בעיקר בירושה ומאפשר לכיתות ילדים לעשות שימוש חוזר או לשנות נתונים ופונקציות תוך שמירה עליהם מוסתרים מהעולם החיצון.
CPP
   #include          using     namespace     std  ;   class     Employee     {   private  :         double     salary  ;   protected  :         int     empID  ;   public  :         string     name  ;      Employee  (  string     n       double     s       int     id  )     {      name     =     n  ;      salary     =     s  ;      empID     =     id  ;      }   };   // Derived class (inherits from Employee)   class     Manager     :     public     Employee     {   public  :      Manager  (  string     n       double     s       int     id  )     :     Employee  (  n       s       id  )     {}      void     showDetails  ()     {      cout      < <     'Manager Name: '      < <     name      < <     endl  ;         cout      < <     'Manager ID: '      < <     empID      < <     endl  ;         }   };   int     main  ()     {      Employee     emp  (  'Fedrick'       50000       101  );      cout      < <     'Employee Name: '      < <     emp  .  name      < <     endl  ;      Manager     m  (  'Rohit'       70000       102  );      m  .  showDetails  ();         return     0  ;   }   

תְפוּקָה
Employee Name: Fedrick Manager Name: Rohit Manager ID: 102  


פֶּתֶק : גישה זו דרך ירושה יכולה לשנות את שינוי הגישה של האלמנטים של מחלקה הבסיסית במחלקה נגזרת בהתאם ל- אופן הירושה .

צור חידון