马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 这是她 于 2020-5-14 22:33 编辑
Although the world is full of suffering,it is also full of the overcoming of it.
运算符重载
加法运算符重载------实现两个自定义数据类型相加的运算
1、对于内置函数的数据类型的表达式运算符是不可能改变的;
2、不要滥用运算符重载 <font color="black">#include<iostream>
using namespace std;
class Student
{
public:
int m_A;
int m_B;
Student() {};
//成员函数实现两个对象相加
Student Add(Student &p)
{
Student temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
//通过成员函数进行重载+号
Student operator+ (Student &p)
{
Student temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
};
//通过全局函数进行函数重载
Student operator+(Student &p1, Student &p2)
{
Student temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
//函数重载的版本
Student operator+(Student &p1,int num)
{
Student temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
int main()
{
//内置数据类型
int a = 10;
int b = 10;
int c = a + b;
cout << "Value : c : " << c << endl;
Student c1;
c1.m_A = 88;
c1.m_B = 22;
Student c2;
c2.m_A = 66;
c2.m_B = 44;
//成员函数重载本质调用
Student c3 = c1.operator+(c2);
Student c3 = c1 + c2;
cout << "Value : c3.m_A : " << c3.m_A << endl;
cout << "Value : c3.m_B : " << c3.m_B << endl;
//全局变量重载本质调用
Student c3 = operator+(c1,c2);
Student c4 = c1 + c2;
cout << "Value : c4.m_A : " << c4.m_A << endl;
cout << "Value : c4.m_B : " << c4.m_B << endl;
//成员函数调用相加
Student c5 = c1.Add(c2);
cout << "Value : c5.m_A : " << c5.m_A << endl;
cout << "Value : c5.m_B : " << c5.m_B << endl;
//运算符重载,函数重载
Student c6 = operator+(c1,100);
cout << "Value : c6.m_A : " << c6.m_A << endl;
cout << "Value : c6.m_B : " << c6.m_B << endl;
return 0;
}</font>
左移运算符------可以输出自定义数据类型
#include<iostream>
using namespace std;
class Person
{
friend ostream & operator<<(ostream &cout,Person &p);
private:
int m_A;
int m_B;
public:
Person(int a,int b)
{
this->m_A = a;
this->m_B = b;
}
};
//不能利用成员函数重载<<运算符,因为无法实现cout在左侧
//void operator<<(cout)-----------p1 << cout
//只能利用全局变量函数重载左移运算符
//cout只能有一个,所以用& 引用方式
//返回值为ostream-----cout属于ostream输出流
ostream & operator<<(ostream &cout,Person &p)//本质:operator<<(cout,p1)-----cout << p1
{
cout << "Value : m_A -> " << p.m_A << endl;
cout << "Value : m_B -> " << p.m_B << endl;
return cout;
}
int main()
{
Person p1(99,66);
cout << p1 << endl;
return 0;
}
递增运算符重载------实现自己的整型变量
前置递增返回的是引用,后置递增返回的是值;#include<iostream>
using namespace std;
class Library
{
friend ostream & operator<<(ostream &cout,Library p);
private:
int m_num;
public:
Library(int a)
{
this->m_num = a;
}
//重载前置++运算符
//返回引用---为了一直对一个数据进行递增操作
Library& operator++()
{
//先进行++运算
m_num++;
//再将自身做返回
return *this;
}
//重载后置++运算符
//int代表占位参数,可以用于区分前置和后置递增
//返回的是一个值
Library operator++(int)
{
//先记录当前结果
Library temp = *this;
//在递增
m_num++;
//最后将记录结果做返回
return temp;
}
};
ostream & operator<<(ostream &cout,Library p)
{
cout << "Value : m_num : " << p.m_num << endl;
return cout;
}
int main()
{
Library y1(100);
cout << ++y1 << endl;
cout << y1 << endl;
Library y2(100);
cout << y2++ << endl;
cout << y2 << endl;
return 0;
}
赋值运算符重载
C++编译器至少给一个类添加4个函数:
1、默认构造函数(无参,函数体为空);
2、默认析构函数(无参,函数体为空);
3、默认拷贝函数,对属性进行值拷贝;
4、赋值运算符operator=,对属性进行值拷贝;#include<iostream>
using namespace std;
class Student
{
public:
//定义一个指针
int *m_grade;
Student(int grade)
{
//将成绩数据开辟到堆区
m_grade = new int(grade);
}
//析构函数-->在这里来释放在堆区开辟的内存
~Student()
{
if(m_grade != NULL)
{
delete m_grade;
m_grade = NULL;
}
}
//重载 赋值运算符
//由于返回对象->在这里返回值是类名
//返回是引用->可以继续进行赋值操作
Student& operator=(Student &p)
{
//浅拷贝
//m_grade = p.m_grade;
//先判断一下对象是否有属性在堆区;有->释放
if(m_grade != NULL)
{
delete m_grade;
m_grade = NULL;
}
//深拷贝
m_grade = new int(*p.m_grade);
//返回对象本身
return *this;
}
};
int main()
{
int a = 10;
int b = 10;
int c = 10;
cout << "Value : a -> " << a << endl;
cout << "Value : b -> " << b << endl;
cout << "Value : c -> " << c << endl;
Student s1(10);
Student s2(20);
Student s3(30);
s3 = s2 = s1;
cout << "Value : s1 -> " << *s1.m_grade << endl;
cout << "Value : s2 -> " << *s2.m_grade << endl;
cout << "Value : s3 -> " << *s3.m_grade << endl;
return 0;
}
关系运算符重载---重载关系运算符,可以让两个自定义类型对象进行对比操作#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
string m_name;
int m_num;
Student(string name,int num)
{
m_name = name;
m_num = num;
}
//重载关系运算符 ==
bool operator==(Student &p)
{
//满足姓名和学号相等
if(this->m_name == p.m_name && this->m_num == p.m_num)
{
return true;
}
return false;
}
//重载关系运算符 !=
bool operator!=(Student &p)
{
if(this->m_name == p.m_name && this->m_num == p.m_num)
{
return false;
}
return true;
}
};
int main()
{
Student p1("张三",3333);
Student p2("张三",3333);
Student p3("李四",3333);
if (p1 == p2)
{
cout << "p1和p2是相等的!!!!" << endl;
}
else
{
cout << "p1和p2是不相等的!!!" << endl;
}
if (p1 == p3)
{
cout << "p1和p3是相等的!!!!" << endl;
}
else
{
cout << "p1和p3是不相等的!!!!" << endl;
}
if (p1 != p2)
{
cout << "p1和p2是不相等的!!!!" << endl;
}
else
{
cout << "p1和p2是相等的!!!" << endl;
}
if (p1 != p3)
{
cout << "p1和p3是不相等的!!!!" << endl;
}
else
{
cout << "p1和p3是相等的!!!!" << endl;
}
return 0;
}
函数调用运算符重载 ()-------由于重载使用的方法非常像函数的调用->仿函数(没有固定的写法,非常灵活)#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
//重载函数调用运算符 ()
void operator()(string something)
{
cout << something << endl;
}
};
void kuohao(string something)
{
cout << something << endl;
}
//仿函数非常灵活,例如下面的乘
class Mymul
{
public:
int operator()(int num1,int num2)
{
return num1 * num2;
}
};
int main()
{
Student p1;
//由于使用起来非常类似于函数调用,一次成为仿函数
p1("Although the world is full of suffering,it is also full of the overcoming of it.");
kuohao("Although the world is full of suffering,it is also full of the overcoming of it.");
cout << "----------------------------------------------------------------------------" << endl;
Mymul p2;
int a = p2(10,10);
cout << "a = " << a << endl;
//匿名函数对象----一个匿名对象,又重载()---当前行执行完,立即释放
cout << Mymul()(100,10) << endl;
return 0;
}
渣渣一个但在努力中哦.........................................
|