1. 在類域外部(非類域中),類對象訪問自身接口和成員的限制,示例如下。 [cpp] #include <stdlib.h> #include <iostream> using namespace std; class fruit { public: fruit(int id, int count, int price) :m_count(count), m_price(price), m_id(id) { } int count() { return m_count; } public: int m_count; protected: int price() { return m_price; } protected: int m_price; private: int id() { return m_id; } private: int m_id; }; int main(int argc, char **argv) { fruit FRUIT_1(000, 100, 15); cout << "FRUIT_1.m_count" << FRUIT_1.m_count << endl; cout << "FRUIT_1.count()" << FRUIT_1.count() << endl; cout << "FRUIT_1.m_price" << FRUIT_1.m_price << endl; cout << "FRUIT_1.price()" << FRUIT_1.price() << endl; cout << "FRUIT_1.m_id" << FRUIT_1.m_id << endl; cout << "FRUIT_1.id()" << FRUIT_1.id() << endl; } 編譯時報錯,如下,可見在類域外部,類對象只能訪問聲明為public的接口。 2. 在自身的類域中,類對象訪問自身接口和成員的限制,示例如下: [cpp] #include <stdlib.h> #include <iostream> using namespace std; class fruit { public: fruit(int id, int count, int price) :m_count(count), m_price(price), m_id(id) { } int count() { return m_count; } // 訪問自身類對象的public成員 int other_count_member(fruit other) { return other.m_count; } // 訪問自身類對象的protected成員 int other_price_member(fruit other) { return other.m_price; } // 訪問自身類類對象的private成員 int other_id_member(fruit other) { return other.m_id; } // 訪問自身類對象的public函數 int other_count_func(fruit other) { return other.count(); } // 訪問自身類對象的protected函數 int other_price_func(fruit other) { return other.price(); } // 訪問自身類對象的private函數 int other_id_func(fruit other) { return other.id(); } public: int m_count; protected: int price() { return m_price; } protected: int m_price; private: int id() { return m_id; } private: int m_id; }; int main(int argc, char **argv) { fruit FRUIT_1(111, 100, 10); fruit FRUIT_2(222, 200, 20); cout << "FRUIT_1.other_count_member(FRUIT_2):" << FRUIT_1.other_count_member(FRUIT_2) << endl; cout << "FRUIT_1.other_count_func(FRUIT_2):" << FRUIT_1.other_count_func(FRUIT_2) << endl; cout << "FRUIT_1.other_price_member(FRUIT_2):" << FRUIT_1.other_price_member(FRUIT_2) << endl; cout << "FRUIT_1.other_price_func(FRUIT_2):" << FRUIT_1.other_price_func(FRUIT_2) << endl; cout << "FRUIT_1.other_id_member(FRUIT_2):" << FRUIT_1.other_id_member(FRUIT_2) << endl; cout << "FRUIT_1.other_id_func(FRUIT_2):" << FRUIT_1.other_id_func(FRUIT_2) << endl; } 可以正確編譯,運行結果如下,可見在自身的類域中,類對象的所有成員和接口都是可見的。 3. 在其他類域(非繼承關系)中,類對象訪問自身的接口和成員的限制,示例如下 [cpp] #include <stdlib.h> #include <iostream> using namespace std; class book { public: book(int count, int price, int id) :m_count(count), m_price(price), m_id(id) { } int count() { return m_count; } public: int m_count; protected: int price() { return m_price; } protected: int m_price; private: int id() { return m_id; } private: int m_id; }; class fruit { public: fruit(int id, int count, int price) :m_count(count), m_price(price), m_id(id) { } int count() { return m_count; } // 訪問其他類對象的public成員 int other_count_member(book other) { return other.m_count; } // 訪問其他類對象的public函數 int other_count_func(book other) { return other.count(); } // 訪問其他類對象的protected成員 int other_price_member(book other) { return other.m_price; } // 訪問其他類對象的protected函數 int other_price_func(book other) { return other.price(); } // 訪問其他類類對象的private成員 int other_id_member(book other) { return other.m_id; } // 訪問其他類對象的private函數 int other_id_func(book other) { return other.id(); } public: int m_count; protected: int price() { return m_price; } protected: int m_price; private: int id() { return m_id; } private: int m_id; }; int main(int argc, char **argv) { fruit FRUIT_1(111, 100, 10); book BOOK_1(222, 200, 20); cout << "FRUIT_1.other_count_member(BOOK_1):" << FRUIT_1.other_count_member(BOOK_1) << endl; cout << "FRUIT_1.other_count_func(BOOK_1):" << FRUIT_1.other_count_func(BOOK_1) << endl; cout << "FRUIT_1.other_price_member(BOOK_1):" << FRUIT_1.other_price_member(BOOK_1) << endl; cout << "FRUIT_1.other_price_func(BOOK_1):" << FRUIT_1.other_price_func(BOOK_1) << endl; cout << "FRUIT_1.other_id_member(BOOK_1):" << FRUIT_1.other_id_member(BOOK_1) << endl; cout << "FRUIT_1.other_id_func(BOOK_1):" << FRUIT_1.other_id_func(BOOK_1) << endl; } 編譯時出錯,如下,可見在非繼承關系的類中,其他類對象只有public成員或接口是可見的。 4. 在子類的域中,父類對象的成員和接口的訪問限制,示例如下 (1)訪問自身基類對象的成員 [cpp] #include <stdlib.h> #include <iostream> using namespace std; class fruit { public: fruit(int count, int price, int id) :m_count(count), m_price(price), m_id(id) { } int count() { return m_count; } public: int m_count; protected: int price() { return m_price; } protected: int m_price; private: int id() { return m_id; } private: int m_id; }; class apple:public fruit { public: apple(int count, int price, int id) :fruit (count, price, id) { } // 訪問基類的public成員 int base_public_member() { return m_count; } // 訪問基類的public函數 int base_public_func() { return count(); } // 訪問基類的protected成員 int base_protected_member() { return m_price; } // 訪問基類的protected函數 int base_protected_func() { return price(); } // 訪問基類的private成員 int base_private_member() { return m_id; } // 訪問基類的private函數 int base_private_func() { return id(); } }; int main(int argc, char **argv) { apple APPLE_1(111, 100, 10); cout << "APPLE_1.base_public_member():" << APPLE_1.base_public_member() << endl; cout << "APPLE_1.base_public_func():" << APPLE_1.base_public_func() << endl; cout << "APPLE_1.base_protected_member():" << APPLE_1.base_protected_member() << endl; cout << "APPLE_1.base_protected_func():" << APPLE_1.base_protected_func() << endl; cout << "APPLE_1.base_private_member():" << APPLE_1.base_private_member() << endl; cout << "APPLE_1.base_private_func():" << APPLE_1.base_private_func() << endl; } 編譯報錯,如下,可見默認情況下,自身子類對象只能訪問父類的public,protected 成員和接口,private 接口和成員不可見。 (2)訪問其他基類對象的成員,示例如下 [cpp] #include <stdlib.h> #include <iostream> using namespace std; class fruit { public: fruit(int count, int price, int id) :m_count(count), m_price(price), m_id(id) { } int count() { return m_count; } public: int m_count; protected: int price() { return m_price; } protected: int m_price; private: int id() { return m_id; } private: int m_id; }; class apple:public fruit { public: apple(int count, int price, int id) :fruit (count, price, id) { } // 訪問基類的public成員 int base_public_member(fruit other) { return other.m_count; } // 訪問基類的public函數 int base_public_func(fruit other) { return other.count(); } // 訪問基類的protected成員 int base_protected_member(fruit other) { return other.m_price; } // 訪問基類的protected函數 int base_protected_func(fruit other) { return other.price(); } // 訪問基類的private成員 int base_private_member(fruit other) { return other.m_id; } // 訪問基類的private函數 int base_private_func(fruit other) { return other.id(); } }; int main(int argc, char **argv) { fruit FRUIT_1(111, 100, 10); apple APPLE_1(222, 200, 20); cout << "APPLE_1.base_public_member(fruit FRUIT_1):" << APPLE_1.base_public_member(FRUIT_1) << endl; cout << "APPLE_1.base_public_func(fruit FRUIT_1):" << APPLE_1.base_public_func(FRUIT_1) << endl; cout << "APPLE_1.base_protected_member(fruit FRUIT_1):" << APPLE_1.base_protected_member(FRUIT_1) << endl; cout << "APPLE_1.base_protected_func(fruit FRUIT_1):" << APPLE_1.base_protected_func(FRUIT_1) << endl; cout << "APPLE_1.base_private_member(fruit FRUIT_1):" << APPLE_1.base_private_member(FRUIT_1) << endl; cout << "APPLE_1.base_private_func(fruit FRUIT_1):" << APPLE_1.base_private_func(FRUIT_1) << endl; } 編譯報錯,如下,可見訪問基類的對象時,仿佛訪問的是陌生類的對象一樣,只能訪問其 public 的成員和接口。 5. 父類的域中,子類對象的成員和接口訪問限制 (1)在父類域中訪問子類對象的成員和接口的限制。 [cpp] #include <stdlib.h> #include <iostream> using namespace std; class fruit; class apple:public fruit { public: apple(int count, int price, int id) :m_count(count), m_price(price), m_id(id) { } int count() { return m_count; } public: int m_count; protected: int price() { return m_price; } protected: int m_price; private: int id() { return m_id; } private: int m_id; }; class fruit { public: // 訪問子類的public成員 int derived_public_member(apple &other) { return other.m_count; } // 訪問子類的public函數 int derived_public_func(apple &other) { return other.count(); } // 訪問子類的protected成員 int derived_protected_member(apple &other) { return other.m_price; } // 訪問子類的protected函數 int derived_protected_func(apple &other) { return other.price(); } // 訪問子類的private成員 int derived_private_member(apple &other) { return other.m_id; } // 訪問子類的private函數 int derived_private_func(apple &other) { return other.id(); } }; int main(int argc, char **argv) { fruit FRUIT_1; apple APPLE_1(222, 200, 20); cout << "FRUIT_1.derived_public_member(fruit FRUIT_1):" << FRUIT_1.derived_public_member(APPLE_1) << endl; cout << "FRUIT_1.derived_public_func(fruit FRUIT_1):" << FRUIT_1.derived_public_func(APPLE_1) << endl; cout << "FRUIT_1.derived_protected_member(fruit FRUIT_1):" << FRUIT_1.derived_protected_member(APPLE_1) << endl; cout << "FRUIT_1.derived_protected_func(fruit FRUIT_1):" << FRUIT_1.derived_protected_func(APPLE_1) << endl; cout << "FRUIT_1.derived_private_member(fruit FRUIT_1):" << FRUIT_1.derived_private_member(APPLE_1) << endl; cout << "FRUIT_1.derived_private_func(fruit FRUIT_1):" << FRUIT_1.derived_private_func(APPLE_1) << endl; } 編譯出錯,可見父類域中訪問子類對象的接口,如同陌生類一樣,非public接口均不可訪問。 (2)父類域中,僅僅訪問子類對象的public接口,能否成功呢?如下 [cpp] #include <stdlib.h> #include <iostream> using namespace std; class fruit; class apple:public fruit { public: apple(int count, int price, int id) :m_count(count), m_price(price), m_id(id) { } int count() { return m_count; } public: int m_count; protected: int price() { return m_price; } protected: int m_price; private: int id() { return m_id; } private: int m_id; }; class fruit { public: // 訪問子類的public成員 int derived_public_member(apple &other) { return other.m_count; } // 訪問子類的public函數 int derived_public_func(apple &other) { return other.count(); } //// 訪問子類的protected成員 //int derived_protected_member(apple &other) //{ // return other.m_price; //} //// 訪問子類的protected函數 //int derived_protected_func(apple &other) //{ // return other.price(); //} //// 訪問子類的private成員 //int derived_private_member(apple &other) //{ // return other.m_id; //} //// 訪問子類的private函數 //int derived_private_func(apple &other) //{ // return other.id(); //} }; int main(int argc, char **argv) { fruit FRUIT_1; apple APPLE_1(222, 200, 20); cout << "FRUIT_1.derived_public_member(fruit FRUIT_1):" << FRUIT_1.derived_public_member(APPLE_1) << endl; cout << "FRUIT_1.derived_public_func(fruit FRUIT_1):" << FRUIT_1.derived_public_func(APPLE_1) << endl; //cout << "FRUIT_1.derived_protected_member(fruit FRUIT_1):" << FRUIT_1.derived_protected_member(APPLE_1) << endl; //cout << "FRUIT_1.derived_protected_func(fruit FRUIT_1):" << FRUIT_1.derived_protected_func(APPLE_1) << endl; //cout << "FRUIT_1.derived_private_member(fruit FRUIT_1):" << FRUIT_1.derived_private_member(APPLE_1) << endl; //cout << "FRUIT_1.derived_private_func(fruit FRUIT_1):" << FRUIT_1.derived_private_func(APPLE_1) << endl; } 編譯報錯,說fruit類不完全。看來,要在父類中調用子類的接口是不太可能的了,因為兩者是繼承關系,父類要調用子類的接口,必須先定義子類,但是子類的定義依賴父類的定義,所以這樣就陷入類似死鎖的情況。不過這種需求也是沒有必要的。 總結一下: (1)類域中,可以訪問自身對象和此類的外部對象的所有成員和接口。 (2)繼承關系的類域中,子類只能訪問自身對象的基類的 public 和 protected 成員和接口 (3)繼承關系的類域中,子類僅能訪問其他的基類對象的 public 成員和接口 (4)繼承關系的類域和,父類不能訪問任何子類的成員和接口 (5)非繼承關系的類域 以及 非類域中,僅能訪問類對象的public成員和接口。