首页 > 编程语言 >C++ 《运算符重载》

C++ 《运算符重载》

时间:2024-07-17 16:19:22浏览次数:11  
标签:const temp int age C++ 运算符 operator 重载

示例代码

#include "iostream"

//operator+
using namespace std;
class A{
public:
   int m_age;
public:
    A(){}
    A(int age):m_age(age){}
//    A operator+(const A &a){ //成员函数实现重载
//        A temp(0);
//        temp.m_age = m_age+a.m_age;
//        return temp;
//    }
};

A operator+(const A &a,const A &b){ //全局函数实现重载
    A temp(0);
    temp.m_age = a.m_age+b.m_age;
    return temp;
}
A operator+(const A &a,int val){
    A temp(0);
    temp.m_age = a.m_age+val;
    return temp;
}
void test1(){
    A a1(10);
    A a2(20);
    cout<<(a1+a2).m_age<<endl; // 30
    cout<<(a1+44).m_age<<endl; // 54
}

// operator<< 做一运算符重载(成员函数不能实现)
// 一般用于格式化输出
class Person{
public:
    string m_name;
    int m_age;
public:
    Person(string name,int age):m_name(name),m_age(age){}
};
ostream &operator<<( ostream &out,const Person &p){
    out<<"姓名:"<<p.m_name<<"年龄:"<<p.m_age;
    return out;
}
void test02(){
    Person p("张三",20);
    cout<<p<<endl;
}
// 自增重载
class MyInt{
    friend ostream &operator<<( ostream &out,const MyInt &p);
private:
    int m_Num;
public:
    MyInt(){
        m_Num =0;
    }
    //前置++
    MyInt &operator++(){
        m_Num++;    //先++,再返回
        return *this; //返回对象本身
    }
    //后置++
    MyInt operator++(int ){
        MyInt temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
        m_Num++;
        return temp;
    }
};
ostream &operator<<( ostream &out,const MyInt &p){
    out<<p.m_Num;
    return out;
}

//赋值运算符重载
class Student{
public:
    int *m_age;
     explicit Student(int age){
        m_age = new int(age);
    }
    Student(const Student &s){ //拷贝函数
        m_age = new int(*s.m_age);
    }
    ~Student(){ //析构函数
        if(m_age != nullptr){
            delete m_age;
            m_age = nullptr;
        }
    }
    //重载赋值运算符
    Student &operator=(const Student &s){
        if(this == &s){
            return *this;
        }
        if(m_age != nullptr){
            delete m_age;
            m_age = nullptr;
        }
        m_age = new int(*s.m_age);
        return *this;
    }
    //重载相等
    bool operator==(Student & stu) const{
         if(*m_age == *stu.m_age){
             return true;
         }else{
             return false;
         }
     }
     //重载不等运算符
     bool operator!=(Student & stu) const{
         if(*m_age != *stu.m_age){
             return true;
         }
         return false;
     }

};

int main(){
    test1();
    return 0;
}

标签:const,temp,int,age,C++,运算符,operator,重载
From: https://www.cnblogs.com/paylove/p/18307688

相关文章

  • c/c++ 浅拷贝与深拷贝
    浅拷贝与深拷贝的区别浅拷贝:简单的赋值拷贝操作深拷贝:在堆区重新申请空间,进行拷贝操作默认情况下对象拷贝是浅拷贝(深拷贝要自己实现拷贝函数)classPerson{public: //无参(默认)构造函数 Person(){ cout<<"无参构造函数!"<<endl; } //有参构造函数 Person(in......
  • 高质量C/C++编程指南总结(七)—— 内存管理
    1.内存分配的方式从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static变量。在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处......
  • C++扫雷小代码
    先声明一下:玩法介绍程序会提示输入:<ROW><COL><HEALTH><MINE_SUM>,分别代表行数,列数(均小于等于30),生命值(小于行数×列数),雷数(小于行数×列数),有数据判断操作符qpca+X,Y分别代表:在(x,y)²(见注释)翻开/插旗/撤销插旗/使用Ai(作为起始点)³,以下为详细介绍qxy:翻开位于......
  • 二叉苹果树(C++)
    【题目描述】有一棵二叉苹果树,如果数字有分叉,一定是分两叉,即没有只有一个儿子的节点。这棵树共 N 个节点,标号1 至 N ,树根编号一定为 1 。我们用一根树枝两端连接的节点编号描述一根树枝的位置。一棵有四根树枝的苹果树,因为树枝太多了,需要剪枝。但是一些树枝上长有苹......
  • 小球掉落(C++)
    #include<bits/stdc++.h>usingnamespacestd;structnode{ intid; booldata; intfather; intlson,rson;};nodetree[6000000];intd,i;intmain(){ cin>>d>>i; tree[1].father=0; tree[1].lson=2; tree[1].rson=3; tree[1].data=false;......
  • 找树根和孩子(C++)
    【问题描述】 给定一棵树,输出树的根root,孩子最多的结点max以及他的孩子【输入格式】第一行:n(结点数<=100),m(边数<=200)。以下m行;每行两个结点x和y,表示y是x的孩子(x,y<=1000)。【输出格式】第一行:树根:root。第二行:孩子最多的结点max。第三行:max的孩子,按照从小到大的顺......
  • 基于SSM的校园志愿者管理系统小程序+99213(免费领源码)可做计算机毕业设计JAVA、PHP、爬
    小程序+springboot校园志愿者管理系统摘 要随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,校园志愿者管理系统被用户普遍使用,为方便用户能够可以随时进行在线查看校园志愿......
  • 华为OD机试D卷 --找座位--24年OD统一考试(Java & JS & Python & C & C++)
    文章目录题目描述输入描述输出描述用例题目解析java源码python源码javascript源码c源码c++源码题目描述在一个大型体育场内举办了一场大型活动,由于疫情防控的需要,要求每位观众的必须间隔至少一个空位才允许落座。现在给出一排观众座位分布图,座位中存......
  • 华为OD机试D卷 --密码输入检测--24年OD统一考试(Java & JS & Python & C & C++)
    文章目录题目描述输入描述输出描述用例题目解析java源码python源码javascript源码c源码c++源码题目描述给定用户密码输入流input,输入流中字符‘<’表示退格,可以清除前一个输入的字符,请你编写程序,输出最终得到的密码字符,并判断密码是否满足如下的密......
  • C++ 贪心算法
    理解贪心算法贪心算法采用的是贪心策略在每一步中都采取最优解(局部最优解),以期望得到最终的全局最优解例子#include<iostream>#include<bits/stdc++.h>usingnamespacestd;intmain(){ inta[510]={0};//表示每个人的打水时间的数组 intr,n,s=0;//水......