C++类和对象-C++运算符重载->加号运算符重载、左移运算符重载、递增运算符重载、赋值运算符重载、关系运算符重载、函数调用运算符重载

#include<iostream>

using namespace std;

//加号运算符重载

class Person {

public:

Person() {};

Person(int a, int b)

{

this->m_A = a;

this->m_B = b;

}

//1.成员函数实现 + 号运算符重载

Person operator+(const 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+(const Person& p1, const Person& p2) {

// Person temp(0, 0);

// temp.m_A = p1.m_A + p2.m_A;

// temp.m_B = p1.m_B + p2.m_B;

// return temp;

//}

//运算符重载 可以发生函数重载

Person operator+(const Person& p2, int val)

{

Person temp;

temp.m_A = p2.m_A + val;

temp.m_B = p2.m_B + val;

return temp;

}

void test() {

Person p1(10, 10);

Person p2(20, 20);

//成员函数重载本质调用

Person p3 = p2 + p1; //相当于 p2.operaor+(p1)

cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;

//全局函数重载本质调用

Person p4 = p1 + p2; //相当于 operator+(p1,p2)

cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;

//运算符重载 也可以发生函数重载

Person p5 = p3 + 10; //相当于 operator+(p3,10)

cout << "mA:" << p5.m_A << " mB:" << p5.m_B << endl;

}

int main() {

test();

system("pause");

return 0;

}

总结1:对于内置的数据类型的表达式的的运算符是不可能改变的

总结2:不要滥用运算符重载

#include<iostream>

using namespace std;

//左移运算符重载

class Person

{

friend ostream& operator<<(ostream& cout, Person& p);

public:

Person(int a, int b)

{

this->m_A = a;

this->m_B = b;

}

//利用成员函数重载 左移运算符

//成员函数 实现不了 p << cout 不是我们想要的效果

//void operator<<(Person& p){

//}

private:

int m_A;

int m_B;

};

//只能利用全局函数重载实现左移运算符

//ostream对象只能有一个

ostream& operator<<(ostream& cout, Person& p) //本质 operator<<(cout,p) 简化 cout<<p

{

cout << "a:" << p.m_A << " b:" << p.m_B;

return cout;

}

void test()

{

Person p1(10, 20);

cout << p1 << " hello world" << endl; //链式编程

}

int main()

{

test();

system("pause");

return 0;

}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

#include<iostream>

using namespace std;

//重载递增运算符

//自定义整型

class MyInteger

{

friend ostream& operator<<(ostream& out, MyInteger myint);

public:

MyInteger()

{

m_Num = 0;

}

//重载前置++运算符 返回引用为了一直对一个数据进行递增操作

MyInteger& operator++()

{

//先++

m_Num++;

//再返回

return *this;

}

//重载后置++运算符

//int代表占位参数,可以用于区分前置和后置递增

MyInteger operator++(int)

{

//先 记录当前结果

MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;

//后 递增

m_Num++;

//最后将记录结果做返回

return temp;

}

private:

int m_Num;

};

//重载<<运算符

ostream& operator<<(ostream& out, MyInteger myint)

{

out << myint.m_Num;

return out;

}

//前置++ 先++ 再返回

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;

}

总结: 前置递增返回引用,后置递增返回值

#include<iostream>

using namespace std;

//赋值运算符重载

class Person

{

public:

Person(int age)

{

//将年龄数据开辟到堆区

m_Age = new int(age);

}

//重载赋值运算符

Person& operator=(Person &p)

{

//应该先判断是否有属性在堆区,如果有先释放感觉,然后再深拷贝

if (m_Age != NULL)

{

delete m_Age;

m_Age = NULL;

}

//编译器提供的代码是浅拷贝

//m_Age = p.m_Age;

//提供深拷贝 解决浅拷贝的问题

m_Age = new int(*p.m_Age);

//返回对象自身

return *this;

}

~Person()

{

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; //赋值操作

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;

}

#include<iostream>

using namespace std;

#include<string>

//重载关系运算符

class Person

{

public:

Person(string name, int age)

{

this->m_Name = name;

this->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;

}

}

bool operator!=(Person & p)

{

if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)

{

return false;

}

else

{

return true;

}

}

string m_Name;

int m_Age;

};

void test01()

{

//int a = 0;

//int b = 0;

Person a("孙悟空", 18);

Person b("孙悟空", 18);

if (a == b)

{

cout << "a和b相等" << endl;

}

else

{

cout << "a和b不相等" << endl;

}

if (a != b)

{

cout << "a和b不相等" << endl;

}

else

{

cout << "a和b相等" << endl;

}

}

int main() {

test01();

system("pause");

return 0;

}

#include<iostream>

using namespace std;

#include<string>

//函数调用运算符重载

//打印输出类

class MyPrint

{

public:

//重载函数调用运算符

void operator()(string text)

{

cout << text << endl;

}

};

void myFunc2(string text)

{

cout << text << endl;

}

void test01()

{

//重载的()操作符 也称为仿函数

MyPrint myFunc;

myFunc("hello world");//由于使用起来非常类似于函数调用,因此称为仿函数

myFunc2("hello world");

}

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

//加法类

class MyAdd

{

public:

int operator()(int v1, int v2)

{

return v1 + v2;

}

};

void test02()

{

MyAdd add;

int ret = add(10, 10);

cout << "ret = " << ret << endl;

//匿名函数对象调用

cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;

}

int main() {

test01();

test02();

system("pause");

return 0;

}

相关推荐
YRr YRr13 分钟前
在Windows上安装WSL2和Ubuntu 20.04以搭建C++开发环境的详细指南
c++·windows·ubuntu·wsl2
豆本-豆豆奶13 分钟前
23个Python在自然语言处理中的应用实例
开发语言·python·自然语言处理·编程语音
曳渔19 分钟前
Java-数据结构-二叉树-习题(三)  ̄へ ̄
java·开发语言·数据结构·算法·链表
Farewell_me27 分钟前
C++模拟实现list:list、list类的初始化和尾插、list的迭代器的基本实现、list的完整实现、测试、整个list类等的介绍
c++·list
你可以自己看35 分钟前
python中函数式编程与高阶函数,装饰器与生成器,异常处理与日志记录以及项目实战
服务器·开发语言·python
gopher95111 小时前
go语言 数组和切片
开发语言·golang
ymchuangke1 小时前
线性规划------ + 案例 + Python源码求解(见文中)
开发语言·python
gopher95111 小时前
go语言Map详解
开发语言·golang
Python私教1 小时前
Go语言现代web开发15 Mutex 互斥锁
开发语言·前端·golang
hong1616881 小时前
VSCode中配置C/C++环境
c语言·c++·vscode