c++之重载的基本使用

代码说明:下面重载只有常规的几种使用方式,其中操作符的重载也只写了简单的加减乘除的类内类外的基本使用;后续有时间逐步完善,欢迎指正;

/**
 * 代码内容:c++ 重载的案例文件
 *
 */

#include <iostream>
#include <string.h>

using namespace std;

/**
 * 常规的重载定义:
 * 1、函数名称相同;
 * 2、在同一个作用域中;
 * 3、参数类型、个数、顺序不同的情况;
 * 注意:
 * 1、函数返回值类型不同,不可以作为重载的条件;
 */
class overloadTest_1

{
private:
    /* data */
public:
    void fun1()
    {
        cout << "overload1" << endl;
    }

    void fun1(int num, string name)
    {
        cout << "overload2" << endl;
        cout << "num:" << num << ",name:" << name << endl;
    }

    void fun1(string name, int num, int age)
    {
        cout << "overload2" << endl;
        cout << "num:" << num << ",name" << name << ",age:" << age << endl;
    }
};

// int main(int argc, char const *argv[])
// {
//     overloadTest_1 o1;
//     o1.fun1();
//     o1.fun1(100,"xiaoming");
//     o1.fun1("xiaoming",40,60);
//     return 0;
// }

/**
 * 操作符的重载
 * 1、函数名称由关键字operator和要重载的运算符符号构成,与其他函数一样包含一个返回值类型及参数列表
 * *****返回值类型 operator 运算符(参数);
 * *****括号中的参数可以理解为相加的参数类型,如果只有一个参数,注意this指针的使用,指的是与当前函数相加;
 * 2、友员函数的使用:类内外实现、实现不同对象之间的交换功能;
 * 3、成员函数和非成员函数实现的过程;
 */

//定义正方体类
class cuboid
{
private:
    double x;
    double y;
    double z;

public:
    // 计算体积
    double getVolume()
    {
        return x * y * z;
    }
    // 设置xyz
    void setx(double x)
    {
        this->x = x;
    }

    void sety(double y)
    {
        this->y = y;
    }

    void setz(double z)
    {
        this->z = z;
    }

    /**
     * 重载:加减乘除运算符
     * */

    // 重载加法之两个对象相加
    cuboid operator+(const cuboid &c)
    {
        cuboid cuboid;
        cuboid.x = this->x + c.x;
        cuboid.y = this->y + c.y;
        cuboid.z = this->z + c.z;
        return cuboid;
    }
    // 重载加法之对象与常数的相加:这样写就是有一个缺点就是常数和对象相加的顺序不能交换;
    cuboid operator+(const int b)
    {
        cuboid cuboid;
        cuboid.x = this->x + b;
        cuboid.y = this->y + b;
        cuboid.z = this->z + b;
        return cuboid;
    }
    // 重载之对象与常数相加可以交换,通过友员函数
    friend cuboid operator+(const int b, cuboid &c)
    {
        cuboid cuboid;
        cuboid.x = cuboid.x + b;
        cuboid.y = cuboid.y + b;
        cuboid.z = cuboid.z + b;
        return cuboid;
    }

    cuboid operator-(const cuboid &c)
    {
        cuboid cuboid;
        cuboid.x = this->x - c.x;
        cuboid.y = this->y - c.y;
        cuboid.z = this->z - c.z;
        return cuboid;
    }

    cuboid operator*(const cuboid &c)
    {
        cuboid cuboid;
        cuboid.x = this->x * c.x;
        cuboid.y = this->y * c.y;
        cuboid.z = this->z * c.z;
        return cuboid;
    }

    cuboid operator/(const cuboid &c)
    {
        cuboid cuboid;
        cuboid.x = this->x / c.x;
        cuboid.y = this->y / c.y;
        cuboid.z = this->z / c.z;
        return cuboid;
    }

    // 递增递减实现的重载(一般不常用,先空着)

    // 类外实现的方式:如果希望次操作符重载作为全局函数,需要在类内声明为友员函数,然后类外实现
    friend cuboid operator+(const cuboid &a, const cuboid &b);
};

// 全局重载实现
cuboid operator+(const cuboid &a, const cuboid &b)
{
    cuboid cuboid;
    cuboid.x = a.x+ b.x;
    cuboid.y = a.y +b.y;
    cuboid.z = a.z +b.z;
    return cuboid;
}

int main(int argc, char const *argv[])
{
    // 验证operator+
    cuboid c1;
    cuboid c2;
    cuboid c3, c4, c5, c6, c7;
    int num = 1;

    c1.setx(2.0);
    c1.sety(3.0);
    c1.setz(4.0);

    c2.setx(2.0);
    c2.sety(3.0);
    c2.setz(4.0);

    c3 = c1 + c2;
    c4 = c1 - c2;
    c5 = c1 * c2;
    c6 = c1 / c2;

    // c7 = c6 + num;
    c7 = num + c6; //交换
    cout << "c1 Volume:" << c1.getVolume() << endl;
    cout << "c2 Volume:" << c2.getVolume() << endl;
    cout << "c3 Volume:" << c3.getVolume() << endl;
    cout << "c4 Volume:" << c4.getVolume() << endl;
    cout << "c5 Volume:" << c5.getVolume() << endl;
    cout << "c6 Volume:" << c6.getVolume() << endl;
    cout << "c7 Volume:" << c7.getVolume() << endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值