c++ 类的访问权限探讨(三)

2014-11-24 07:13:37 · 作者: · 浏览: 3
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
#include
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
#include
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:
// 访问子类的