5.18
1、友元
友元函数,在类中声明的成员属性存在一些私有权限,成员函数大都是公共权限
友元即提供一种通过类外函数来访问类中私有属性的能力;
友元函数
#include<iostream>
using namespace std;
//创建类
class Building
{
friend void GoodFriend();
public:
string m_sittingroom;
Building();
private:
string m_bedroom;
};
Building::Building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
//全局函数做访问类
void GoodFriend()
{
Building building;
cout << "好朋友访问:" << building.m_sittingroom << endl;
cout << "好朋友访问:" << building.m_bedroom << endl;
}
void test1()
{
GoodFriend();
}
int main()
{
test1();
system("pause");
return 0;
}
友元类,该类中的成员函数都可以作为另一个类中的私有属性访问
//——友元类
#include<iostream>
using namespace std;
class Building
{
friend class GoodFriend;
public:
string m_sittingroom;
Building();
private:
string m_bedroom;
};
Building::Building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
//友元类
class GoodFriend
{
public:
Building building;
void visit(Building& building);
};
void GoodFriend::visit(Building& building)
{
cout << "好朋友正在访问:" << building.m_sittingroom << endl;
cout << "好朋友正在访问:" << building.m_bedroom << endl;
}
void test1()
{
GoodFriend gf;
Building* building = new Building;
gf.visit(*building);//一个引用做形参的函数,在堆区开辟一块变量传入解引用的值
}
int main()
{
test1();
system("pause");
return 0;
}
成员函数做友元
#define _CRT_SECURE_NO_WARNINGS 1
//成员函数做友元
#include<iostream>
using namespace std;
class Building;
class GoodFriend
{
public:
GoodFriend();
Building* building;
void visit1();
void visit2();
};
class Building
{
friend void GoodFriend::visit1();
public:
Building();
string m_sittingroom;
private:
string m_bedroom;
};
Building::Building()
{
m_bedroom = "卧室";
m_sittingroom = "客厅";
}
GoodFriend::GoodFriend()
{
building = new Building;
}
void GoodFriend::visit1()
{
cout << "好朋友正在访问:" << building->m_sittingroom << endl;
cout << "好朋友正在访问:" << building->m_bedroom << endl;
}
void GoodFriend::visit2()
{
cout << "好朋友正在访问:" << building->m_sittingroom << endl;
//cout << "好朋友正在访问:" << building->m_bedroom << endl;
}
void test1()
{
GoodFriend gf;
gf.visit1();
gf.visit2();
}
int main()
{
test1();
system("pause");
return 0;
}
2、运算符重载
+重载 重载原来的+是两个自定义的变量可以实现相加功能,
//加号运算符重载
#include<iostream>
using namespace std;
class Person
{
public:
//成员函数
//Person operator+(Person& p)
//{
// Person temp;
// temp.m_age = this->m_age + p.m_age;
// return temp;
//}
int m_age;
};
Person operator + (Person& p1,Person& p2)
{
Person temp;
temp.m_age = p1.m_age + p2.m_age;
return temp;
}
Person operator+(Person& p1, int num)
{
Person temp;
temp.m_age = p1.m_age + num;
return temp;
}
void test1()
{
Person p1;
p1.m_age = 18;
Person p2;
p2.m_age = 10;
Person p3 = p1 + p2;
//成员函数核心p3 = p1.operator+(p2);
//全局函数核心p3=p1.operator+(p1,p2);
cout << p3.m_age << endl;
Person p4 = p1 + 50;
cout << p4.m_age << endl;
//核心p4=p1.operator+(p1,50);
}
int main()
{
test1();
system("pause");
return 0;
}
这个地方重载函数传参可以传入引用,也可以直接传入形参,相当于复制一份
<<左移运算符重载
//左移运算符重载
#include<iostream>
using namespace std;
class Person
{
public:
//成员函数进行左移运算符的重载行不通
// 实现一种p<<cout
//void operator<<(ostream& cout)
//{
//}
int m_a;
int m_b;
};
//void operator<<(ostream& cout,Person p)
//{
// cout << "m_a:" << p.m_a << " m_b:" << p.m_b << endl;
//}
//链式思想,输出流后续可以继续输出
ostream& operator<<(ostream& cout, Person p)
{
cout << "m_a:" << p.m_a << " m_b:" << p.m_b << endl;
return cout;
}
void test1()
{
Person p1;
p1.m_a = 10;
p1.m_b = 20;
//cout << p1 ;
//核心operator<<(cout,p1);
cout << p1 << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
++递增运算符重载
//++递增运算符重载
#include<iostream>
using namespace std;
class Myinter
{
friend ostream& operator<<(ostream& cout, Myinter myinter);
public:
Myinter()
{
m_a = 10;
}
//成员函数的前置递增重载
Myinter& operator++()
{
m_a++;
return *this;
}
//成员函数后置递增操作
Myinter& operator++(int)
{
Myinter temp = *this;
m_a++;
return temp;
}
private:
int m_a;
};
ostream& operator<<(ostream& cout,Myinter myinter)
{
cout << myinter.m_a;
return cout;
}
void test1()
{
Myinter myinter;
cout << ++myinter << endl;
cout << myinter++ << endl;
cout << myinter << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
这个地方在区别前置和后置递增时考虑运算的顺序
--递减运算符重载
//递减
#include<iostream>
using namespace std;
class Myinter
{
friend ostream& operator<<(ostream& cout, Myinter myinter);
public:
Myinter()
{
m_a = 10;
}
//前制递减
Myinter& operator--()
{
m_a--;
return *this;
}
//后置递减
Myinter operator--(int)
{
Myinter temp = *this;
m_a--;
return temp;
}
private:
int m_a;
};
ostream& operator<<(ostream& cout, Myinter myinter)
{
cout << myinter.m_a;
return cout;
}
void test1()
{
Myinter myinter;
cout << --myinter << endl;
cout << myinter-- << endl;
cout << myinter << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
=赋值运算符重载
//赋值重载
#include<iostream>
using namespace std;
class Person
{
public:
//void operator=(Person& p)
//{
// m_age = p.m_age;
//}
//Person(int age)
//{
// m_age = age;
//}
//int m_age;
Person& operator=(Person& p)
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
m_age = new int(*p.m_age);
return *this;
}
Person(int age)
{
m_age = new int(age);
}
~Person()
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
}
int* m_age;
};
//ostream& operator<<(ostream& cout, Person p)
//{
// cout << p.m_age;
// return cout;
//}
void test1()
{
Person p1(10);
Person p2(20);
Person p3(30);
cout << *p1.m_age << endl;
p3 = p1 = p2;
cout << *p1.m_age << endl;
cout << *p3.m_age << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
这一块涉及到深浅拷贝的操作
==关系运算符重载
//关系运算符重载
#include<iostream>
using namespace std;
class Person
{
public:
bool operator==(Person& p)
{
if (m_age == p.m_age && m_name == p.m_name)
{
return true;
}
return false;
}
Person(int age, string name)
{
m_age = age;
m_name = name;
}
string m_name;
int m_age;
};
void test1()
{
Person p1(18,"张三");
Person p2(18,"张三");
if (p1 == p2)
{
cout << "俩人相等" << endl;
}
else
{
cout << "俩人不等" << endl;
}
}
int main()
{
test1();
system("pause");
return 0;
}
函数调用运算符重载
//函数调用运算重载
#include<iostream>
using namespace std;
class Myprint
{
public:
void operator()(string test)
{
cout << test << endl;
}
};
class Myadd
{
public:
int operator()(int a, int b)
{
return a + b;
}
int m_a;
int m_b;
};
void test1()
{
Myprint myprint;
myprint("hello");
Myadd myadd;
int c = myadd(3, 5);
cout << c << endl;
}
int main()
{
test1();
system("pause");
return 0;
}