瀏覽代碼

上传文件至 '友元'

Creamo 2 年之前
父節點
當前提交
d4d4595cc6
共有 4 個文件被更改,包括 220 次插入0 次删除
  1. 50 0
      友元/1、全局函数做友元.cpp
  2. 71 0
      友元/2、类做友元.cpp
  3. 83 0
      友元/3、成员函数做友元.cpp
  4. 16 0
      友元/友元.cpp

+ 50 - 0
友元/1、全局函数做友元.cpp

@@ -0,0 +1,50 @@
+//全局函数做友元
+
+#if(0)
+
+#include <iostream>
+
+
+//建筑物类
+class Building
+{
+	//goodGay全局函数是Building好朋友,可以访问Building中私有成员
+	friend void goodGay(Building& building);
+
+public:
+	Building()
+	{
+		m_SittingRoom = "客厅";
+		m_Bedroom = "卧室";
+	}
+
+public:
+	std::string m_SittingRoom;       //客厅
+private:
+	std::string m_Bedroom;           //卧室
+};
+
+//全局函数
+void goodGay(Building &building)
+{
+	std::cout << "好基友的全局函数 正在访问:" << building.m_SittingRoom << std::endl;
+	std::cout << "好基友的全局函数 正在访问:" << building.m_Bedroom << std::endl;
+}
+
+void test01()
+{
+	Building building;
+	goodGay(building);
+}
+
+int main()
+{
+
+	test01(); 
+
+	system("pause");
+
+	return 0;
+}
+
+#endif

+ 71 - 0
友元/2、类做友元.cpp

@@ -0,0 +1,71 @@
+//类做友元
+
+#if(0)
+
+#include <iostream>
+
+class Building;
+
+class GoodGay
+{
+public:
+
+	GoodGay();
+
+	void visit();       //参观函数  访问Building中的属性
+	
+	Building* building;
+};
+
+class Building
+{
+	//GoodGay这个类是本类的好朋友
+	friend class GoodGay;
+
+public:
+	Building();
+
+
+public:
+	std::string m_SittingRoom;           //客厅
+private:
+	std::string m_BedRoom;               //卧室
+
+};
+
+//类外写成员函数:
+Building::Building()
+{
+	m_SittingRoom = "客厅";
+	m_BedRoom = "卧室";
+}
+
+GoodGay::GoodGay()
+{
+	//创建建筑物对象
+	building = new Building;
+}
+
+void GoodGay::visit()
+{
+	std::cout << "好基友类正在访问:" << building->m_SittingRoom << std::endl;
+	std::cout << "好基友类正在访问:" << building->m_BedRoom << std::endl;
+}
+
+void test01()
+{
+	GoodGay gg;
+	gg.visit();
+}
+
+int main()
+{
+
+	test01();
+
+	system("pause");
+
+	return 0;
+}
+
+#endif

+ 83 - 0
友元/3、成员函数做友元.cpp

@@ -0,0 +1,83 @@
+//4.4.3   成员函数做友元
+
+
+
+#include <iostream>
+
+class Building;
+
+class GoodGay
+{
+public:
+	GoodGay();
+
+	void visit();                   //让visit函数可以访问Building中私有成员
+
+	void visit2();                 //让visit2函数不可以访问Building中私有成员
+	
+
+	Building* building;
+};
+
+class Building
+{
+	//告诉编译器  GoodGay类下的visit成员函数作为本类的好朋友,可以访问私有成员
+	friend void GoodGay::visit();
+
+public:
+
+	Building();
+
+public:
+	
+	std::string m_SittingRoom;        //客厅
+
+private:
+
+	std::string m_BedRoom;         //卧室
+
+};
+
+//类外实现成员函数
+
+Building::Building()
+{
+	this->m_BedRoom = "卧室";
+	this->m_SittingRoom = "客厅";
+}
+
+GoodGay::GoodGay()
+{
+	building = new Building;
+}
+
+void GoodGay::visit()
+{
+	std::cout << "visit函数正在访问:" << building->m_SittingRoom << std::endl;
+
+	std::cout << "visit函数正在访问:" << building->m_BedRoom << std::endl;
+}
+
+void GoodGay::visit2()
+{
+	std::cout << "visit2函数正在访问:" << building->m_SittingRoom << std::endl;
+
+	//std::cout << "visit2函数正在访问:" << building->m_BedRoom << std::endl;
+}
+
+void test01()
+{
+	GoodGay gg;
+	gg.visit();
+	gg.visit2();
+}
+
+int main()
+{
+	test01();
+	
+
+	system("pause");
+
+	return 0;
+}

+ 16 - 0
友元/友元.cpp

@@ -0,0 +1,16 @@
+//4.4   友元
+//生活中你家有客厅(Public),有你的卧室(Private)
+//客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你能进去
+//但是呢,你也可以允许你的好闺蜜好基友进去。
+
+//在程序里,有些私有属性 也想让类外特殊的一些函数或者类进行访问 ,就需要用到友元的技术
+
+//友元的目的就是让一个函数或者类 访问另一个类中私有成员
+
+//友元关键字为 friend
+
+//友元的三种实现
+//⚪全局函数做友元
+//⚪类做友元
+//⚪成员函数做友元
+