c++ 类的访问权限探讨(二)
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
#include
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
#include
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,