友元:
在程序里,有些私有属性,也想让类外特殊的一些函数或者类进行访问,就需要用到友元技术
友元的关键字为 friend
三种实现
全局函数做友元
class Building
{
//goodgay全局函数是Building好朋友,可以访问Building中私有成员
friend void goodgay(Building & building);
public:
Building()
{
m_sittingroom = "客厅";
m_Bedroom = "卧室";
}
public:
string m_sittingroom;
private:
string m_Bedroom;
};
void goodgay(Building &building)
{
cout << building.m_sittingroom << endl;
cout << building.m_Bedroom << endl;
}
类做友元
class Building
{
//Goodgay类是Building好朋友,可以访问Building中私有成员
friend class Goodgay;
public:
Building()
{
m_sittingroom = "客厅";
m_Bedroom = "卧室";
}
public:
string m_sittingroom;
private:
string m_Bedroom;
};
class Goodgay
{
public:
Goodgay()
{
builing = new Building;
}
void visit()
{
cout << builing->m_sittingroom << endl;
cout << builing->m_Bedroom << endl;
}
Building* builing;
};
void test()
{
Goodgay gg;
gg.visit();
}
成员函数做友元
运算符重载
对已有的运算符进行重新定义,赋予其另一种功能,以适应不同的数据类型
通过成员函数实现加号重载
class Person
{
public:
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;
}
public :
int m_a;
int m_b;
};
void test()
{
Person p1;
p1.m_a = 10;
p1.m_b = 10;
Person p2;
p2.m_a = 10;
p2.m_b = 10;
Person p3 = p1 + p2;
cout << p3.m_a << endl; //20
cout << p3.m_b << endl; //20
}
通过全局函数实现
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;
}
左移运算符的重载
重载左移运算符配合友元可以实现输出自定义数据类型
不会利用成员函数来实现,因为只能实现<<在左边,只能利用全局函数
class Person
{
friend ostream& operator<< (ostream& cout, Person& p);
private :
int m_a;
int m_b;
};
ostream & operator<< (ostream &cout,Person &p)
{
cout << "m_a = " << p.m_a << "m_b = " << p.m_b ;
return cout;
}
递增运算符重载
通过重载递增运算符,实现自己的整形数据
//前置
MyInteger operator++()
{
//先++
m_Num++;
///在返回
return *this;
}
//后置
MyInteger operator++(int)//int为了区分两个函数的重名
{
//先返回
MyInteger temp = *this;//记录当前本身的值加1,但是返回的是以前的值,达到先返回后++
m_Num++;
return temp;
}
赋值运算符重载
c++编译器至少给一个类添加4个函数
1.默认构造函数
2.默认析构函数
3.默认拷贝构造函数,对属性进行值拷贝
4.赋值运算符operator=对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
class Person
{
public:
Person(int age)
{
m_age = new int(age);
}
~Person()
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
}
//重载 赋值运算符
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;
}
int* m_age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1;
cout << *p1.m_age << endl;
cout << *p2.m_age << endl;
cout << *p3.m_age << endl;
}
关系运算符重载
可以让两个自定义的对象进行对比操作
class Person
{
public:
Person(string name, int age)
{
m_name = name;
m_age = age;
}
bool operator==(Person& p)
{
if (this->m_name == p.m_name && this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
void test01()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2)
{
cout << "两人相等" << endl;
}
else
{
cout << "不相等" << endl;
}
}
函数调用运算符重载
函数调用运算符()也可以重载
由于重载后使用的方式非常像函数的调用,因此称为仿函数
仿函数没有固定写法,非常灵活
class Myprint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
void test01()
{
Myprint p1;
p1("hello world");
}