一、友元
1. 全局函数做友元
- 友元的目的是让一个函数或者类 访问另一个类中的私有成员
- 友元的关键字为
friend
- 友元的三种实现
- 全局函数做友元
- 类做友元
- 成员函数做友元
代码测试:
#include<iostream>
using namespace std;
#include<string>
//建筑物类
class Building
{
//goodGay全局函数是Building好朋友,可以访问Building中私有成员
friend void goodGay(Building* building);
public:
Building()
{
m_BedRoom = "卧室";
m_SittingRoom = "客厅";
}
public:
string m_SittingRoom;//客厅
private:
string m_BedRoom;//卧室
};
//全局函数
void goodGay(Building *building)
{
cout << "好基友全局函数 正在访问:" << building->m_SittingRoom << endl;
cout << "好基友全局函数 正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
Building building;
goodGay(&building);
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
好基友全局函数 正在访问:客厅
好基友全局函数 正在访问:卧室
请按任意键继续. . .
2. 友元类
代码测试:
#include<iostream>
using namespace std;
#include<string>
//建筑物类
class Building;
class GoodGay
{
public:
GoodGay();
void visit();//参观函数 访问Building中的属性
Building* building;
};
//建筑物类
class Building
{
//GoodGay类是Building好朋友,可以访问Building中私有成员
friend class GoodGay;
public:
Building();
public:
string m_SittingRoom;//客厅
private:
string m_BedRoom;//卧室
};
//类外写成员函数
Building::Building() //首先调用构造函数
{
m_BedRoom = "卧室";
m_SittingRoom = "客厅";
}
GoodGay::GoodGay() //首先调用构造函数
{
//创建建筑物对象
building = new Building;
}
void GoodGay::visit()
{
cout << "好基友类正在访问:" << building->m_SittingRoom << endl;
cout << "好基友类正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
好基友类正在访问:客厅
好基友类正在访问:卧室
请按任意键继续. . .
3. 成员函数做友元
代码测试:
#include<iostream>
using namespace std;
#include<string>
class Building;
class GoodGay
{
public:
GoodGay();
void visit();//让visit函数可以访问Building中私有成员
void visit2();//让visit函数不可以访问Building中私有成员
Building* building;
};
class Building
{
//告诉编译器 GoodGay类下的visit成员函数作为本类的好朋友,
//可以访问私有成员
friend void GoodGay::visit();
public:
Building();
string m_SittingRoom;
private:
string m_BedRoom;
};
//类外实现成员函数
Building::Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
building = new Building;
}
void GoodGay::visit()
{
cout << "visit 函数正在访问:" << building->m_SittingRoom << endl;
cout << "visit 函数正在访问:" << building->m_BedRoom << endl;
}
void GoodGay::visit2()
{
cout << "visit2 函数正在访问:" << building->m_SittingRoom << endl;
//cout << "visit 函数正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
gg.visit2();
}
int main()
{
test01();
system("pause");
return 0;
}
输出;
visit 函数正在访问:客厅
visit 函数正在访问:卧室
visit2 函数正在访问:客厅
请按任意键继续. . .
二、运算符重载
- 对已有的运算符重载进行定义,赋予其另一种功能,以适应不同的数据类型。
1. 加号运算符重载
两个自定义的数据类型怎么相加?
例如:
Person p3 = p1.operator(p2);
//简化为
Person p3 = p1 + p2;
代码测试1:
#include<iostream>
using namespace std;
#include<string>
//加号运算符重载
class Person
{
public:
//1、成员函数重载+号
/*Person operator+ (Person& p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}*/
int m_A;
int m_B;
};
//2、全局函数重载+号
Person operator+(Person& p1, Person& p2)
{
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
void test01()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
//成员函数重载本质调用
//Person p3 = p1.operator+(p2);
//全局函数重载本质调用
Person p3 = operator+(p1, p2);
//只要写了加号重载,可以直接写,不用写明调用了什么重载方法
//Person p3 = p1 + p2;
cout << "p3.m_A = " << p3.m_A << " p3.m_B = " << p3.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
p3.m_A = 20 p3.m_B = 20
请按任意键继续. . .
代码测试2:
运算符重载也可以发生函数重载
#include<iostream>
using namespace std;
#include<string>
//加号运算符重载
class Person
{
public:
//1、成员函数重载+号
/*Person operator+ (Person& p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}*/
int m_A;
int m_B;
};
//2、全局函数重载+号
Person operator+(Person& p1, Person& p2)
{
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
//函数重载版本
Person operator+(Person& p1, int num)
{
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
void test01()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
//成员函数重载本质调用
//Person p3 = p1.operator+(p2);
//全局函数重载本质调用
//Person p3 = operator+(p1, p2);
//只要写了加号重载,可以直接写,不用写明调用了什么重载方法
Person p3 = p1 + p2;
cout << "p3.m_A = " << p3.m_A << endl;
cout << "p3.m_B = " << p3.m_B << endl;
//运算符重载,也可以发生函数重载
Person p4 = p1 + 100;
cout << "p4.m_A = " << p4.m_A << endl;
cout << "p4.m_B = " << p4.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
p3.m_A = 20
p3.m_B = 20
p4.m_A = 110
p4.m_B = 110
请按任意键继续. . .
- 总结:
- 对于内置的数据类型的表达式的运算符是不能改变的
- 不能滥用运算符重载,加号就实现加法重载,减号就实现减号重载…
2. 左移运算符重载
可以输出自定义数据类型
比如:
Person p;
p.m_A = 10;
p.m_B = 10;
cout<< p << endl;//可以自动输出Person数据类型p的m_A和m_B
代码测试1:
#include<iostream>
using namespace std;
#include<string>
//左移运算符重载
class Person
{
public:
//成员函数实现 左移运算符 p.operator<<(cout) 简化版本 p<<cout
//通常不会用成员函数实现左移运算符重载,因为无法实现 cout在左侧
/*void operator<<(cout)
{
}*/
int m_A;
int m_B;
};
//只能通过全局函数实现左移运算符重载
//本质 operator<<(cout,p),简化版本为 cout << p
//全局只能有一个cout,所以用&
void operator<< (ostream &cout, Person &p)
{
cout << "m_A = " << p.m_A << " m_B = " << p.m_B << endl;
}
void test01()
{
Person p;
p.m_A = 10;
p.m_B = 10;
cout << p;
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
m_A = 10 m_B = 10
请按任意键继续. . .
代码测试2:
#include<iostream>
using namespace std;
#include<string>
//左移运算符重载
//为了一次输出Person这个对象里面的多个属性
class Person
{
friend ostream& operator<< (ostream& cout, Person& p);
public :
Person(int a,int b)
{
m_A = a;
m_B = b;
}
private:
//成员函数实现 左移运算符 p.operator<<(cout) 简化版本 p<<cout
//通常不会用成员函数实现左移运算符重载,因为无法实现 cout在左侧
/*void operator<<(cout)
{
}*/
int m_A;
int m_B;
};
//只能通过全局函数实现左移运算符重载
//本质 operator<<(cout,p),简化版本为 cout << p
//全局只能有一个cout,所以用&
//加上ostream & ,用了链式编程思想
//cout换成out也可以,因为引用的本质是起别名
ostream & operator<< (ostream &cout, Person &p)
{
cout << "m_A = " << p.m_A << " m_B = " << p.m_B ;
return cout;
}
void test01()
{
Person p(10,10);
/*p.m_A = 10;
p.m_B = 10;*/
//cout << p<< endl;//加上endl后报错,用到链式编程思想
cout << p << " hello world" << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
m_A = 10 m_B = 10 hello world
请按任意键继续. . .
3. 递增运算符重载
通过递增运算符重载,实现自己的整型数据。
代码测试:
#include<iostream>
using namespace std;
#include<string>
//递增运算符重载
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符
//返回一个引用,为了一直对一个数据进行递增
MyInteger& operator++()
{
m_Num++;
return *this;
}
//重载后置递增运算符
//(int)是一个占位参数,可以用于区分前置递增和后置递增
//后置递增一定要返回值,而不是返回引用,因为temp是一个局部变量
MyInteger operator++(int)
{
//先记录当时结果
MyInteger temp = *this;
//后递增
m_Num++;
//最后返回记录的当时的结果
return temp;
}
private:
int m_Num;
};
//重载左移运算符
ostream& operator<<(ostream& cout, MyInteger myint)
{
cout << myint.m_Num;
return cout;
}
void test01()
{
MyInteger myint;
cout << ++(++myint) << endl;
cout << myint << endl;
}
void test02()
{
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
输出:
2
2
0
1
请按任意键继续. . .
4. 赋值运算符重载
C++编译器至少给一个类添加4个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
- 赋值运算符 operator= ,对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深拷贝问题。
代码测试:
#include<iostream>
using namespace std;
#include<string>
//赋值运算符重载
class Person
{
public:
Person(int age)
{
cout << "构造函数调用" << endl;
m_Age = new int(age);
}
//重载 赋值运算符
Person & operator=(Person &p)
{
//编译器提供浅拷贝
//m_Age = p.m_Age;
//应该先判断是否有属性在堆区,如果有先是否干净,然会再深拷贝
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
//深拷贝
m_Age = new int(*p.m_Age);
return *this;
}
~Person()
{
cout << "析构函数调用" << endl;
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
int* m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1;
//p2 = p1;//赋值操作
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main()
{
test01();
int a = 10;
int b = 20;
int c = 30;
c = b = a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
system("pause");
return 0;
}
输出:
构造函数调用
构造函数调用
构造函数调用
p1的年龄为:18
p2的年龄为:18
p3的年龄为:18
析构函数调用
析构函数调用
析构函数调用
a = 10
b = 10
c = 10
请按任意键继续. . .
5. 关系运算符重载
- 作用:可以让两个自定义类型对象进行比较
代码测试:
#include<iostream>
using namespace std;
#include<string>
//关系运算符重载
class Person
{
public:
Person(string name,int age)
{
m_Name = name;
m_Age = age;
}
bool operator==(Person &p)
{
if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
{
return true;
}
return false;
}
bool operator!=(Person& p)
{
if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
{
return false;
}
return true;
}
string m_Name;
int m_Age;
};
void test01()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2)
{
cout << "p1 和 p2 相等" << endl;
}
else
{
cout << "p1 和 p2 不相等" << endl;
}
if (p1 != p2)
{
cout << "p1 和 p2 不相等" << endl;
}
else
{
cout << "p1 和 p2 相等" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
输出:
p1 和 p2 相等
p1 和 p2 相等
请按任意键继续. . .
6. 函数调用运算符重载
- 函数调用运算符()也可以重载
- 由于重载后使用方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
代码测试:
#include<iostream>
using namespace std;
#include<string>
//函数调用运算符重载
class MyPrint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
void MyPrint02(string test)
{
cout << test << endl;
}
void test01()
{
MyPrint myPrint;
//传给了void operator()(string test)
myPrint("hello world");//由于使用起来非常像函数调用,称为仿函数
MyPrint02("hello world");//函数调用
}
//仿函数没有固定写法,非常灵活
//加法类
class MyAdd
{
public:
int operator()(int num1,int num2)
{
return num1 + num2;
}
};
void test02()
{
MyAdd myAdd;
cout << myAdd(18, 25) << endl;
//匿名函数对象 MyAdd()(100, 100)
//MyAdd()是匿名对象
cout << MyAdd()(100, 100) << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
输出:
hello world
hello world
43
200
请按任意键继续. . .