首页 > 编程语言 > 详细

(10)C++ 使用类

时间:2019-09-27 16:04:24      阅读:64      评论:0      收藏:0      [点我收藏+]

  一、运算符重载

1.

#include<iostream>
using namespace std;

class Sum {
    int add;
public:
    Sum(int add) {
        this->add = add;
    }
    int operator+(const Sum & sum) const {
        int res;
        res = this->add + sum.add;
        return res;
    }
    int show() {
        return add;
    }
};
int main() {
    Sum sum1(10);
    Sum sum2(20);
    int c=sum1.operator+(sum2);
    cout << c << endl;
    int d = sum1 + sum2; //等同于
    cout << d << endl;
}

 

2.运算符重载限制

重载运算符时操作数至少有一个是用户定义的类型

3.不能创建新运算符

4.不能重载以下运算符:

sizeof

.     .*    ::    ?:

typeid    const_cast   dynamic_cast

reinterpret_cast   static_cast

5.可重载的运算符

+  -  *  /  %  ^  &  |  ~=  !  =  <  >  +=  -=  *=  /=  %=  ^=  &=  |=

<<  >>  >>=  <<=  ==  !=  <=  >=  &&  ||  ++  --  ,  ->*  ->

()  []  new  delete  new[]  delete[]

 

 

二、友元

 

(10)C++ 使用类

原文:https://www.cnblogs.com/buchizaodian/p/11597960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!