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

QT学习(五)C++函数重载

一、 函数重载

在同一个作用域内,可以声明几个功能类似的同名函数,
这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。您不能仅通过返回类型的不同来 重载函数。
下面的实例中,同名函数 print() 被用于输出不同的数据类型
#include <iostream>
#include "string"
using namespace std;
class printData
{
public:
    void print(int i) {
         cout << "整数为: " << i << endl;
    }
    void print(double f) {
        cout << "浮点数为: " << f << endl;
    }
    void print(string str) {
        cout << "字符串为: " << str << endl;
    }
};
void print(int i) {
    cout << "整数为: " << i << endl;
}
void print(double f) {
    cout << "浮点数为: " << f << endl;
}
void print(string str) {
    cout << "字符串为: " << str << endl;
}

void print(int i,double f) {
     cout << "整数为: " << i <<",浮点数为:"<<f<< endl;
}

int main(void)
{
    print(5);
    print(500.13);
    print("hello world");
    print(1,23.2);
    printData pd;
    // 输出整数
    pd.print(5);
    // 输出浮点数
    pd.print(500.263);
    // 输出字符串
    string str = "Hello C++";
    pd.print(str);
    return 0;
}

二、 运算符重载

C++ 中,运算符重载是一个允许程序员自定义各种运算符(如 + , - , == , != 等)在自定义类型(类或结构体)上的行为的特性。这意味着你可以定义类似于内置类型的运算符行为,使你的自定义类型更加直观和易于使用。
基本原则
1. 不可以创建新的运算符 :只能重载已经存在的运算符。
2. 至少有一个操作数是用户定义的类型 :不能重载两个基本类型的运算符。
3. 不能更改运算符的优先级 :重载的运算符保持其原有的优先级和结合性。
示例 1 :假设我们有一个 Person 类,我们可以重载 == 运算符来实现两个 Person 是否相等的判断。
#include <iostream>
using namespace std;
class Person
{
public:
    string name;
    int inNumberTail;
    bool operator==(Person pTmp);
};

bool Person::operator==(Person pTmp){
    return pTmp.name == name && pTmp.inNumberTail == inNumberTail;
}
int main()
{
    //假设我们认定名字和身份证尾号6位一样的两个对象是同一个人!
    Person p1;
    p1.name = "张三";
    p1.inNumberTail = 412508;
    Person p2;
    p2.name = "张三";
    p2.inNumberTail = 412508;
    bool ret = p1 == p2;
    cout << ret << endl;
    return 0;
}
示例 2 :假设我们有一个简单的 Point 类,我们可以重载 + 运算符来实现两个点的加法。
#include <iostream>
using namespace std;
class Point {
public:
    int x, y;
    // 重载 + 运算符
    Point operator+(Point other) {
    Point res;
    res.x = x + other.x;
    res.y = y + other.y;
    return res;
    }
};
int main() {
    Point p1;
    p1.x = 1;
    p1.y = 2;
    Point p2;
    p2.x = 2;
    p2.y = 3;
    Point p3 = p1 + p2; // 使用重载的 + 运算符
    std::cout << "p3.x: " << p3.x << ", p3.y: " << p3.y << std::endl; // 输出
    return 0;

};


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

相关文章:

  • CISCRISC? CPU架构有哪些? x86 ARM?
  • lnmp指令
  • 【5G NR】移动通讯中使用的信道编解码技术
  • 入门指南|Chat GPT 的兴起:它如何改变数字营销格局?
  • 《MySQL 简易速速上手小册》第9章:高级 MySQL 特性和技巧(2024 最新版)
  • vue3 之 商城项目—home
  • 《Docker极简教程》--Docker基础--基础知识(二)
  • Mac上几款好用的MacBook视频播放器
  • C++一维数组
  • c# DataTable 帮助类
  • 无人机在化工消防救援中的应用,消防无人机应用场景分析
  • 洛谷使用指南
  • 【GameFramework框架】四、GameFramework框架内置模块
  • 第6章 智能租房——前期准备
  • windows10安装配置nvm以达到切换nodejs的目的
  • vscode 无法远程连接waiting the server log
  • [leetcode] 32. 最长有效括号
  • 【Git】Windows下通过Docker安装GitLab
  • 计算机自顶向下 Wireshark labs——DNS
  • Arcgis使用过程中常见问题解决方法
  • ArcGIS学习(六)地理数据库
  • 恒创科技:怎么看云主机的性价比
  • rust语言tokio库底层原理解析
  • Linux查询指令
  • PneumoLLM:少样本大模型诊断尘肺病新方法
  • Android 13.0 系统framework修改低电量关机值为3%
  • Python HttpServer 之 简单快速搭建本地服务器,并且使用 requests 测试访问下载服务器文件
  • 医学图像隐私保护
  • OCP使用CLI创建和构建应用
  • vue3 之 商城项目—登陆