Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ความแตกต่างระหว่าง Private และ Protected ใน C++

ในบทความนี้ เราจะทำความเข้าใจความแตกต่างระหว่าง private และ protected access modifiers ใน C++

Private Access Modifier

  • ประกาศด้วยคีย์เวิร์ด 'private' ตามด้วย ':'
  • ไม่สามารถเข้าถึงได้จากภายนอกคลาส
  • คีย์เวิร์ด 'private' เป็น access modifier ที่รับประกันว่าฟังก์ชันและแอตทริบิวต์ภายในคลาสจะถูกเข้าถึงได้เฉพาะโดยสมาชิกของคลาสที่ประกาศไว้
  • อนุญาตให้เฉพาะ member functions หรือ friend functions เท่านั้นที่เข้าถึงข้อมูลที่ติดป้าย 'private' ได้

ตัวอย่าง

#include <iostream> 
using namespace std; 
class base_class{ 
    private: 
    string my_name; 
    int my_age; 
    public: 
    void getName(){ 
        cout << "Enter the name... "; 
        cin >> my_name; 
        cout << "Enter the age... "; 
        cin >> my_age; 
    } 
    void printIt(){ 
        cout << "The name is : " << my_name << endl; 
        cout << "The age is: " << my_age << endl; 
    } 
}; 
int main(){ 
    cout<<"An object of class is created"<< endl; 
    base_class my_instance; 
    my_instance.getName(); 
    my_instance.printIt(); 
    return 0; 
}

ผลลัพธ์

/tmp/u5NtWSnX5A.o An object of class is created Enter the name... Jane Enter the age... 34 The name is : Jane The age is: 34

Protected Access Modifier

  • Protected access modifier คล้ายกับ private access modifier
  • ประกาศด้วยคีย์เวิร์ด 'protected' ตามด้วย ':'
  • สมาชิกของคลาสที่ประกาศว่า 'Protected' ไม่สามารถเข้าถึงได้จากภายนอกคลาส
  • สามารถเข้าถึงได้ภายในคลาสที่ประกาศไว้
  • ยังสามารถเข้าถึงได้โดย derived class ที่คลาสแม่มีสมาชิก 'protected'
  • ใช้ขณะทำงานกับแนวคิด inheritance

ตัวอย่าง

#include <iostream> 
using namespace std; 
class base_class{ 
    private: 
    string my_name; 
    int my_age; 
    protected: 
    int my_salary; 
    public: 
    void getName(){ 
        cout << "Enter the name... "; 
        cin >> my_name; 
        cout << "Enter the age... "; 
        cin >> my_age; 
    } 
    void printIt(){ 
        cout << "The name is : " << my_name << endl; 
        cout << "The age is: " << my_age << endl; 
    } 
}; 
class derived_class : public base_class{ 
    private: 
    string my_city; 
    public: 
    void set_salary(int val){ 
        my_salary = val; 
    } 
    void get_data_1(){ 
        getName(); 
        cout << "Enter the city... "; 
        cin >> my_city; 
    } 
    void print_it_1(){ 
        cout << "The salary is: " << my_salary << endl; 
        printIt(); 
        cout << "The city is: " << my_city << endl; 
    } 
}; 
int main(){ 
    cout<<"Instance of derived class is being created.."<<endl; 
    derived_class my_instance_2 ; 
    my_instance_2.set_salary(100); 
    my_instance_2.get_data_1(); 
    my_instance_2.print_it_1(); 
    return 0; 
}

ผลลัพธ์

/tmp/u5NtWSnX5A.o Instance of derived class is being created.. Enter the name... Jane Enter the age... 23 Enter the city... NewYork The salary is: 100 The name is : Jane The age is: 23 The city is: NewYork