类做友元

1.类做友元

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

//类做友元
class Home;

class Goodfriend
{
public:
	Goodfriend();
	Home *home;
	void vist();//参观函数,访问home的属性
};

class Home
{
	//Goodfriend类是本类的好朋友,可以访问本类中私有成员
	friend class Goodfriend;
public:
	Home();

public:
	string m_sittingroom;
private:
	string m_bedroom;

};

//类外写成员函数
Home::Home()
{
	m_sittingroom="客厅";
	m_bedroom = "卧室";
}

Goodfriend::Goodfriend()
{
	//创建home对象
	home = new Home;
}

void Goodfriend::vist()
{
	cout << "好朋友类正在访问:" << home->m_sittingroom << endl;
	cout << "好朋友类正在访问:" << home->m_bedroom << endl;
}
void test1()
{
	Goodfriend boy;
	boy.vist();
}
int main()
{
	test1();
	system("pause");
	return 0;
}

 


版权声明:本文为sang_12345原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。