C++和C的区别3——操作符重载&默认参数

一、操作符重载

C++认为一切操作符都是函数

函数是可以重载的,但并不是所有的运算符都可以重载。

当我们重载了+后,就可以实现Complex的加法运算了。
重点是,operator+add是等价的,operator+就是一个函数名。
但是add的话只有一种调用方式,但是operator+有2种。

操作符重载

二、默认参数

  • 单个默认参数
  • 多个默认参数

默认值是从右往左默认的,并且中间不能跳跃
实参的个数 + 默认参数的个数 >= 形参的个数

三、默认参数与函数重载的关系

  • 默认参数和函数重载不能同时使用,容易引起二义性

比如print(1),它不知道是调用上面的print()还是第二个print()


博客示例源代码

  • 操作符重载
#include <iostream>
#include <vector>
#include <thread>
#include <list>
#include <mutex>

using namespace std;

struct Complex
{
    float real;
    float image;
};

Complex operator+ (Complex a, Complex b)
{
    Complex c;
    c.real = a.real + b.real;
    c.image = a.image + b.image;

    return c;
}

Complex add (Complex a, Complex b)
{
    Complex c;
    c.real = a.real + b.real;
    c.image = a.image + b.image;

    return c;
}

int main()
{
    Complex aa = { 1, 2 }, bb = { 2, 3 };

    Complex cc = aa + bb;
    // Complex cc = operator+ (aa, bb);
    // Complex cc = add (aa, bb);

    cout << "cc = " << "(" << cc.real << ", " << cc.image << ")" << endl;

    return 0;
}

  • 加入默认参数前
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <time.h>

using namespace std;

void weatherCast(string w) 
{
    time_t t = time(0);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A ", localtime(&t));
    cout << tmp << "today is weahter " << w << endl;
}

int main()
{
    weatherCast("PM2.5");
    weatherCast("PM2.5");
    weatherCast("PM2.5");
    weatherCast("PM2.5");
    weatherCast("PM2.5");
    weatherCast("SunShine");
    weatherCast("PM2.5");
    weatherCast("PM2.5");
    weatherCast("PM2.5");
    weatherCast("PM2.5");
    weatherCast("PM2.5");

    return 0;
}
  • 增加默认参数后

#include <iostream>
#include <time.h>

using namespace std;

void weatherCast(string w = "PM2.5")
{
    time_t t = time(0);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A ", localtime(&t));
    cout << tmp << "today is weahter " << w << endl;
}

int main()
{
    weatherCast();
    weatherCast();
    weatherCast("Windy");
    weatherCast();

    return 0;
}
  • 增加默认参数(多个参数)
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <time.h>

using namespace std;

void weatherCast(string w = "PM2.5")
{
    time_t t = time(0);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A ", localtime(&t));
    cout << tmp << "today is weahter " << w << endl;
}

int volume(int l = 9, int w = 8, int h = 5)
{
    return l * w * h;
}

int main()
{
    /*weatherCast();
    weatherCast();
    weatherCast("Windy");
    weatherCast();*/

    cout << volume(3) << endl;
    cout << volume(1, 7) << endl;
    cout << volume(2, 7, 5) << endl;

    return 0;
}

  • 默认参数和函数重载不要同时使用
#include <iostream>
#include <time.h>

using namespace std;

void print(int a)
{
    printf("void print(int a)\n");
}

void print(int a, int b = 10)
{
    printf("void print(int a, int b = 10)\n");
}

int main()
{
    // print(1);
    print(1, 2);
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容