操作符重载(上)

使用C++标准库

C++标准库并不是C++语言的一部分
C++标准库是由C++语言编写而成的类库和函数的集合
C++标准库中定义的类和对象都位于std命名空间中
C++标准库的头文件都不带.h后缀
C++标准库涵盖了C库的功能
C库中<name.h>头文件对应C++中的 < cname>

C++标准库预定义了多数常用的数据结构,如:字符串,链表,队列,栈等

<bitset>
<deque>
<list>
<map>
<queue>
<set>
<stack>
<vector>
#include <cstdio>

using namespace std;

int main()
{
	printf("Hello World!\n");

	printf("Press any key to continue...");

	getchar();

	return 0;
}

Cin和Cout

在这里插入图片描述

#include <iostream>

using namespace std;

int main()
{
	cout<<"Hello World"<<endl; //cout为显示器对象 endl换行

    int x;
	int y;

	cout<<"1st Parameter: ";
	cin>>x; //cin为键盘对象

	cout<<"2nd Parameter: ";
	cin>>y;

	cout<<"Result: "<<x+y<<endl;

	return 0;
}

在这里插入图片描述

C++做了什么?对于<<和>>

左移运算符 << 和 右移运算符 >> 在C语言中只能用于整数运算,并且语义是确定不变的

问题:C++是怎么改变左移运算符和右移运算符的语义的?

第一步:

#include <cstdlib>
#include <iostream>

using namespace std;

//复数 
struct Complex
{
    int a; //实部 
    int b; //虚部 
};

int main(int argc, char *argv[])
{
    Complex c1 = {1, 2};
    Complex c2 = {3, 4};
    Complex c3 = c1 + c2; //ERROR
    
    cout << "Press the enter key to continue ...";
    cin.get(); //等价于getchar()
	 
    return EXIT_SUCCESS;
}

在这里插入图片描述

第二步: add()函数

#include <cstdlib>
#include <iostream>

using namespace std;

struct Complex
{
    int a;
    int b;
};

Complex add(const Complex& c1, const Complex& c2)
{
    Complex ret;
    
    ret.a = c1.a + c2.a;
    ret.b = c1.b + c2.b;
    
    return ret;
}

int main(int argc, char *argv[])
{
    Complex c1 = {1, 2};
    Complex c2 = {3, 4};
    Complex c3 = add(c1, c2);
    
    cout<<"c3.a = "<<c3.a<<endl;
    cout<<"c3.b = "<<c3.b<<endl;
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

在这里插入图片描述

add函数可以解决Complex变量相加的问题,但是Complex是现实世界中确实存在的复数,并且复数在数学中的地位和普通的实数相同

为什么不能让 + 操作符也支持复数相加呢?

第三步:

#include <cstdlib>
#include <iostream>

using namespace std;

struct Complex
{
    int a;
    int b;
};

Complex operator+ (const Complex& c1, const Complex& c2) //operator+相当于add()函数
{
    Complex ret;
    
    ret.a = c1.a + c2.a;
    ret.b = c1.b + c2.b;
    
    return ret;
}

int main(int argc, char *argv[])
{
    Complex c1 = {1, 2};
    Complex c2 = {3, 4};
    Complex c3 = operator+(c1, c2); //c1+c2写法等价,更加直观
    
    cout<<"c3.a = "<<c3.a<<endl;
    cout<<"c3.b = "<<c3.b<<endl;
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

在这里插入图片描述

分析:operator+(c1, c2);调用不直观,直接改为c1+c2,也能编译通过。 Complex c3 = c1+c2);

C++中操作符重载的本质

C++中通过operator关键字可以利用函数扩展操作符
operator的本质是通过函数重载实现操作符重载

用operator关键字扩展的操作符可以用于类吗?

小插曲:友元

C++中的类的友元
private声明使得类的成员不能被外界访问
但是通过friend关键字可以例外的开放权限

friend Complex operator+ (const Complex& c1, const Complex& c2);

<<运算符重载

ostream& operator<< (ostream& out, const Complex& c) //返回ostream&是为了支持链式调用
{
    out<<c.a<<" + "<<c.b<<"i"; //输出复数格式a+bi
    
    return out;
}

第4步:最终的代码

#include <cstdlib>
#include <iostream>

using namespace std;

class Complex
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }               
    
    //友元: operator+函数是本类 Complex的友元 
    friend Complex operator+ (const Complex& c1, const Complex& c2);
    friend ostream& operator<< (ostream& out, const Complex& c);
};

//重载操作符:<< 
ostream& operator<< (ostream& out, const Complex& c)
{
    out<<c.a<<" + "<<c.b<<"i";
    
    return out;
}

//重载操作符:+
Complex operator+ (const Complex& c1, const Complex& c2)
{
    Complex ret;
    
    ret.a = c1.a + c2.a;
    ret.b = c1.b + c2.b;
    
    return ret;
}

int main(int argc, char *argv[])
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2;  //c1+c2 <==> operator+ (c1, c2);
     
    cout<<c1<<endl; //cout为类ostream的一个对象    cout<<c1 <==> operator<< (cout, c1) ;    
					//operator<< (cout, c1) 的返回值为 ostream&,继续 <<endl 
    cout<<c2<<endl;
    cout<<c3<<endl;
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

在这里插入图片描述

C++是怎么改变左移运算符和右移运算符的语义的?

C++中的操作符重载可以改变左移和右移运算符的语义!

//复习添加:重载operator+

//重载 operator+
enum EEE{
    AAA,
    BBB
};
int operator+ (EEE eee){ //注意: overloaded 'operator+' must have at least one parameter of class or enumeration type
    return 777;
}

小结:

操作符重载是C++的强大特性之一
操作符重载的本质是通过函数扩展操作符的语义
operator关键字是操作符重载的关键
friend关键字可以对函数或类开发访问权限
操作符重载遵循函数重载的规则

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值