当前位置: 首页 > article >正文

day4 C++

作业

取余运算符重载

#include <iostream>

using namespace std;

class Stu
{
    //使用friend关键字是全局函数operator%可以访问类私有成员变量
    friend const Stu operator%(const Stu &other1,const Stu &other2);
private:
    int a;
    int b;
public:
    Stu(){}
    Stu(int a,int b):a(a),b(b){}

    //成员函数完成取余
//    const Stu operator%(const Stu &other) const
//    {
//        Stu temp;
//        temp.a=a%other.a;
//        temp.b=b%other.b;
//        return temp;
//    }

    void show()
    {
        cout << "a=" << a <<endl;
        cout << "b=" << b <<endl;
    }
};

//全局函数实现取余
const Stu operator%(const Stu &other1,const Stu &other2)
{
    Stu temp;
    temp.a=other1.a % other2.a;
    temp.b=other1.b % other2.b;
    return temp;
}

int main()
{
    Stu s1(2,4);
    Stu s2(3,3);
    Stu s3=s1%s2;
    s3.show();
    return 0;
}

乘法运算符重载

#include <iostream>

using namespace std;

class Stu
{

    friend const Stu operator*(const Stu &other1,const Stu &other2);
private:
    int a;
    int b;
public:
    Stu(){}
    Stu(int a,int b):a(a),b(b){}
//    const Stu operator*(const Stu &other) const
//    {
//        Stu temp;
//        temp.a=a*other.a;
//        temp.b=b*other.b;
//        return temp;
//    }

    void show()
    {
        cout << "a=" << a <<endl;
        cout << "b=" << b <<endl;
    }
};

const Stu operator*(const Stu &other1,const Stu &other2)
{
    Stu temp;
    temp.a=other1.a * other2.a;
    temp.b=other1.b * other2.b;
    return temp;
}

int main()
{
    Stu s1(2,4);
    Stu s2(3,3);
    Stu s3=s1*s2;
    s3.show();
    return 0;
}


http://www.kler.cn/news/288940.html

相关文章:

  • 【STM32】IIC
  • mongodb 在 Windows 环境下迁移数据库的问题
  • Linux:手搓shell
  • 解析淘宝商品详情API返回值中的特殊属性
  • python系列教程231——__init__.py
  • Docker php文件本地包含--pearcmd.php利用
  • Prometheus+Grafana监控数据可视化
  • 反序列化漏洞(一)
  • flume系列之:批量并行启动、停止、重启flume agent组
  • 设计模式之中介者模式
  • Pixelmator Pro for Mac 专业图像处理软件【媲美PS的修图软件】
  • 微信小程序 - 自定义头部导航栏开发
  • 【区块链 + 物联网】车载终端可信分账应用 | FISCO BCOS应用案例
  • 使用docker部署tensorrtllm推理大模型baichuan2-7b
  • 安装包丨WebGIS开发环境搭建及所需工具
  • 2024-MongoDB中国用户大会
  • Pandas 17-行操作和列操作
  • JavaWeb - Vue项目
  • 软考高级网络规划设计师含金量高吗?
  • 数据传输安全——混合加解密(国密)
  • 使用安信可Ai-WB2-12F开启wifi与手机通信TCP-IP(AT指令)
  • 在Spring框架中,如何实现依赖注入?请列举几种注入方式。请解释Spring Boot的自动配置特性,并讨论其如何简化Web应用开发。
  • 释放GPU潜能:PyTorch中torch.nn.DataParallel的数据并行实践
  • PhpStorm下调试功能配置
  • 【C语言】结构体新的理解
  • css重置样式表 reset.css 格式化默认css样式
  • JavaWeb基础 -- SpringMVC请求和响应
  • Unity 3D学习资料集合
  • 山东大学OLED透明展示柜案例:科技赋能,创新展示新体验
  • 使用HTTP代理注意的点