【C++】函数重载、运算符重载

C++ 重载运算符和重载函数

C++ 允许在同一作用域中的某个函数运算符指定多个定义,分别称为函数重载运算符重载

重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不相同。

当您调用一个重载函数重载运算符时,编译器通过把您所使用的参数类型与定义中的参数类型进行比较,决定选用最合适的定义。选择最合适的重载函数或重载运算符的过程,称为重载决策

1、函数重载的目的

使得多种不同类型的变量可以共用一个函数体,不用重复写很多次相同的函数体(美其名曰提高代码复用率)。

2、运算符重载的目的

1、方便对类类型进行自定义标准的比较。

2、sort()排序用到,方便对n维数组的其中一维、结构体成员变量、类成员变量进行比较

3、函数重载的模板和应用

在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。您不能仅通过返回类型的不同来重载函数。

#include <iostream>
using namespace std;
 
class printData
{
   public:
      void print(int i) {
        cout << "整数为: " << i << endl;
      }
 
      void print(double  f) {
        cout << "浮点数为: " << f << endl;
      }
 
      void print(char c[]) {
        cout << "字符串为: " << c << endl;
      }
};
 
int main(void)
{
   printData pd;
 
   // 输出整数
   pd.print(5);
   // 输出浮点数
   pd.print(500.263);
   // 输出字符串
   char c[] = "Hello C++";
   pd.print(c);
 
   return 0;
}

4、运算符重载的模板和应用

#include <iostream>
using namespace std;

class Box
{
    double length;  // 长度
    double width;   // 宽度
    double height;  // 高度

public:
    Box() {
        length = 0.0;
        width = 0.0;
        height = 0.0;
    }

    Box(double a, double b, double c)
    {
        length = a;
        width = b;
        height = c;
    }

    double getVolume(void)
    {
        return length * width * height;
    }

    // 重载 + 运算符,用于把两个 Box 对象相加
    Box operator+(const Box& b)
    {
        Box box;
        box.length = this->length + b.length;
        box.width = this->width + b.width;
        box.height = this->height + b.height;
        return box;
    }
};

class Complex
{
public:
    double real, imag;
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { }
    Complex operator - (const Complex& c);
};
Complex operator + (const Complex& a, const Complex& b)
{
    return Complex(a.real + b.real, a.imag + b.imag); //返回一个临时对象
}
Complex Complex::operator - (const Complex& c)
{
    return Complex(real - c.real, imag - c.imag); //返回一个临时对象
}


int main()
{
    Box b1(5.0, 4.0, 3.0);
    Box b2(6.0, 5.0, 4.0);
    Box b3;

    cout << "Volume of b1 : " << b1.getVolume() << endl;
    cout << "Volume of b2 : " << b2.getVolume() << endl;

    // 把两个对象相加,得到 Box3
    b3 = b1 + b2;//b1 + b2等价于b1.operator - (b2),返回一个临时变量,b1的值不会被修改~

    // Box3 的体积
    cout << "Volume of b3 : " << b3.getVolume() << endl;

    /****example2******/
    Complex a(4, 4), b(1, 1), c;
    c = a + b; //等价于 c = operator + (a,b);
    cout << c.real << "," << c.imag << endl;
    cout << (a - b).real << "," << (a - b).imag << endl; //a-b等价于a.operator - (b)
    cout << a.real << "," << a.imag << endl; //返回一个临时对象,a.real不会因此被修改!

    return 0;
}

//返回一个临时对象,a.real不会因此被修改!

#include <iostream>
#include <cstring>
using namespace std;
class String {
private:
    char* str;    /*要编写一个长度可变的字符串类 String,该类有一个 char* 类型的成员变量,用以指向动态分配的存储空间,该存储空间用来存放以\0结尾的字符串。*/
public:
    String() :str(NULL) { }//初始化,str指针为NULL
    const char* c_str() const { return str; };//c_str() 的函数声明。返回str指针(指向字符串首地址)
    String& operator = (const char* s);//=运算符重载,传入指针,返回String字符串的首地址
    ~String();//析构函数
};
String& String::operator = (const char* s)
//重载"="以使得 obj = "hello"能够成立
{
    if (str)
        delete[] str;
    if (s) { //s不为NULL才会执行拷贝
        str = new char[strlen(s) + 1];//str指向一个新建的字符数组(字符串即字符数组,不过以\0结尾)
        strcpy(str, s);//把s数组拷贝到str所指的数组中。
    }
    else
        str = NULL;
    return *this;//返回指向String类的指针
}
String::~String()
{
    if (str)
        delete[] str;
};
int main()
{
    String s1;
    String s2;
    s1 = "Good Luck,"; //等价于 s.operator=("Good Luck,");
    cout << s1.c_str() << endl;
    // String s2 = "hello!"; //这条语句要是不注释掉就会出错
    s2 = "Shenzhou 8!"; //等价于 s.operator=("Shenzhou 8!");
    cout << s2.c_str() << endl;
    //s1 = s2;//s1.operator=("Shenzhou 8!")
    //cout << s1.c_str() << endl;
    //cout << s2.c_str() << endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值