C++ 语法 - 8. 运算符重载 operator - 详解

#include <iostream>
#include <string>

using namespace std;

//1.运算符重载
//a.运算符重载 是对已有的运算符 指定新功能。不能创建新运算
//b.运算符重载关键字operator
//c.弄懂运算符的运算对象的个数。(个数决定了 重载函数的参数个数)
//d.识别运算符左边的运算对象 是类的对象 还是其他。
//类的对象:全局函数实现(不推荐) 成员函数实现(推荐,少一个参数)
//其他:只能是全局函数实现

class Person
{
    //如果使用全局函数 重载运算符 必须将全局函数设置成友元。
    friend ostream& operator<<(ostream &out, Person ob);
    friend istream& operator>>(istream& in, Person& ob);
    //friend Person operator+(Person ob1, Person& ob2);

public:
    Person operator+(Person& ob);   //成员函数重载+
    bool operator==(Person &ob);    //成员函数重载==
    Person operator++(int);         //案例1:重载后置++
    Person operator++();            //案例2:重载前置++

private:
    int num;
    string name;
    float score;
public:
    Person(){}
    Person(int num, string name, 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值