您的位置:首页 > 编程语言 > C语言/C++

C++ Primer(第五版) 第十五章 友元和继承

2018-01-29 16:52 176 查看
//THIS IS A TEST!

#include<iostream>
#include<vector>
#include<string>
using namespace std; using std::vector; using std::string;

class Base
{
friend class Pal;
public :
void pub_mem()
{
cout << "Its pub_mem." << endl;
}
protected:
int prot_mem = 99;
private:
char priv_mem = 'K';
};

class Sneaky :public Base
{
friend void clobber(Sneaky&);   //能访问 Sneaky::prot_mem
friend void clobber(Base&);     //不能访问 Base::prot_mem
int j;
};

class Pal
{
public:
int f(Base b) { return b.prot_mem; }

int f3(Sneaky s) { return s.prot_mem; }
};

int main()
{

}
面向对象的程序设计,哪些类,谁能用?被谁用?怎么用?
上面如果不在 class Base中加入友元friend class Pal。Pal就只能使用类Base中的公开声明。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐