C++基础语法9(运算符重载)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


#include<iostream>
using namespace std;
#include<string>
#include<time.h>

//运算符重载:对已有运算符进行重新定义,赋予其另一种功能,以适应不同数据类型
//对于内置的数据类型编译器会正常进行运算 但是自定义的数据类型编译器初始是不会进行运算的!!!!!所以要运算符重载(类也是一种数据类型!)


//加号运算符重载 + :两个自定义数据类型相加运算
//加法函数编译器给的函数名称 : operator+ -----案例见照片!
//运算符重载也可以发生函数重载!
//对于内置的数据类型是不能改变的!!!!!
//不能滥用运算符重载!!!!!

//class Person
//{
//public:
// //1、通过成员函数重载加号 本质上是:Person p3=p1.operator+(p2);!!!!!
// /*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;
//
//};
//
// //2、通过全局函数重载加号 本质上是Person operator+(p1,p2);!!!!!
//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 = 20;
// p2.m_B = 30;
//
// //Person p3 = p1 + p2; // 立马报错!!!!!目前编译器不知道操作符运算
// //写完加号运算符重载之后知道了加法运算!!!!!
// Person p3 = p1 + p2;//所以此代码可以显示成功!!!!!
//
// cout << "p1+p2当中: " << "m_A=: " << p3.m_A << endl;
// cout << "p1+p2当中: " << "m_B=: " << p3.m_B << endl;
//}
//
//int main()
//{
// test01();
// return 0;
//}


//左移运算符重载 << :可以输出自定义的数据类型

//class Person
//{
// friend ostream & operator<<(ostream &cout, Person &p);//友元类的思想
//
//public:
// Person(int a, int b)
// {
// m_A = a;
// m_B = b;
// }
//public:
// //1、利用成员函数来重载 左移运算符!
// //void operator<<(Person &p) //一个函数不知道要返回 什么类型可以先 定义为 void 类型 通常不会利用成员函数来重载 左移运算符!!!!!
// //{
// // //无法实现cout在<<的左侧!!!!! p.operator<<(cout)会变成 p<<cout 发生错误!!!!!
// //}
//private:
// int m_A;
// int m_B;
//};
//
////只能利用全局函数重载左移运算符!!!!! operator需要重载的运算符()
//ostream & operator<<(ostream &cout, Person &p) //本质是 operator<<(cout,p) 简化成 cout<<p ; cout是属于 ostream(输出流)的数据类型的!!!!!
//{
// cout << "m_A = " << p.m_A << " m_B = " << p.m_B << endl;//实现同时输出 p当中的所有内容!!!!!重新定义cout左移运算符<<
// return cout; //cout只是变量名称 可以更改的!!!!! -----引用本质是取别名 指向的是同一块内存空间 所有可以改名!!!!!
//}
//
//void test01()
//{
// Person p(10, 90);
// cout << p << endl;//一次调用 cout<<p 后返回的类型再次调用 <<endl ;实现换行!!!!! 链式思想!!!!!
//}
//int main()
//{
// test01();
// return 0;
//}


//递增运算符的重载 ++ :前置递增(++a) 和后置递增(a++)

//class MyInteger
//{
// //友元
// friend ostream& operator<<(ostream& cout, MyInteger myint);
//public:
// MyInteger()
// {
// m_Num = 0;//构造函数为赋值
// }
//
////重载++运算符
//
// //1、前置递增重载
// MyInteger& operator++() //想让内部的自定义的整形数实现加一当地功能!!!!! m_Num++ 因为是++自身操作 所以最后返回还是myint
// // (返回引用是因为是为了一直对一个数进行++操作)!!!!!
// {
// //先进行++运算
// m_Num++;
// //再将自身做一个返回!
// return *this;//this是指向自身的 *this 即指向++后的m_Num?????
// }
//
// //2、后置递增
// MyInteger operator++(int) //operator++()会发生重定义的现象! int 代表的是占位参数!可以用于区分前置和后置递增!!!!!
// {
// //先 记录当前结果
// MyInteger temp = *this;//*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;
//
//}
//
//void test02()
//{
// MyInteger myint;
// cout << myint++ << endl;
// cout << myint << endl;
//}
//int main()
//{
// //test01();
// test02();
// return 0;
//}



//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; //this是指向自身的指针 *this 是解引用的操作!!!!!
//
// }
//
// int *m_Age;
//};
//
//void test01()
//{
// Person p1(18);
//
// Person p2(20);
//
// p2 = p1;//赋值操作!
//
// cout << "p1的年龄为 " << *p1.m_Age << endl;
//
// cout << "p2的年龄为 " << *p2.m_Age << endl;
//}
//int main()
//{
// test01();
// return 0;
//}


//关系运算符重载:>/</==/!=

//class Person
//{
//public:
// Person(string name,int age) //构造函数赋值
// {
// m_Name = name;
// m_Age = age;
// }
//
//
////重载关系运算符==号
// bool operator==(Person &p)//判断这个类当中原来的属性和传入的 p2是否会相等!!!!!重载==号,判断两个自定义的数据类型!
// {
// if (this->m_Name == p.m_Name&&this->m_Age == p.m_Age)
// {
// return true; //因为返回的是 真 与 假 类型 !所以应该要用 bool类型!
// }
// else
// {
// return false;
// }
// }
//
////重载关系运算符!=号
// bool operator!=(Person &p)//判断这个类当中原来的属性和传入的 p2是否会相等!!!!!重载==号,判断两个自定义的数据类型!
// {
// if (this->m_Name == p.m_Name&&this->m_Age == p.m_Age)
// {
// return false; //因为返回的是 真 与 假 类型 !所以应该要用 bool类型!
// }
// else
// {
// return true;
// }
// }
//
//
// string m_Name;
// int m_Age;
//};
//
//void test01()
//{
// Person p1("张三", 18);
//
// Person p2("王五", 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();
// return 0;
//}


//函数调用运算符重载() :由于重载之后使用方式非常像函数调用,以此也叫-----仿函数!!!!!其 没有固定写法 !非常灵活
class MyPrint //创建一个类 打印

{

public:

void operator()(string test)

{

cout << test << endl;

}

};

void test01()

{

MyPrint myPrint;
myPrint("hellow world"); //由于重载之后使用方式非常像外部函数调用,以此也叫-----仿函数
}

//仿函数非常灵活,没有固定的写法

//加法

class MyAdd

{

public:

int operator()(int num1,int num2)

{

return num1 + num2;

}

};

void test02()

{

MyAdd myAdd;

cout << myAdd(10, 90) << endl;
//匿名函数对象 :类名()(函数重载的运算符)!!!!!
cout << "2 :" << MyAdd()(100, 100) << endl;
}

int main()

{

//test01();

test02();

return 0;

}



阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: c++