C++类中的函数重载:操作符重载

类中的函数重载

(1)函数重载
在这里插入图片描述
(2)类中的函数分类:
构造函数
普通(const)成员函数
static 静态成员函数

那么类中的这些函数之间可以重载吗?
是可以的,但是类中的函数不能和全局函数重载,因为作用域不同。
在这里插入图片描述

#include <stdio.h>

class Test
{
    int i;
public:
    Test()
    {
        printf("Test::Test()\n");
        this->i = 0;
    }
    
    Test(int i)
    {
        printf("Test::Test(int i)\n");
        this->i = i;
    }
    
    Test(const Test& obj)//拷贝构造函数
    {
        printf("Test(const Test& obj)\n");
        this->i = obj.i;
    }
    
    static void func()//静态成员函数
    {
        printf("void Test::func()\n");
    }
    
    void func(int i)//普通成员函数
    {
        printf("void Test::func(int i), i = %d\n", i);
    }
    
    int getI()
    {
        return i;
    }
};

void func()
{
    printf("void func()\n");
}

void func(int i)
{
    printf("void func(int i), i = %d\n", i);
}

int main()
{    
    Test t;        // Test::Test()无参构造函数
    Test t1(1);    // Test::Test(int i)一个参数的构造函数
    Test t2(t1);   // Test(const Test& obj)拷贝构造
    
    func();        // 全局函数:void func()
    Test::func();  // void Test::func()//静态成员函数
    
    func(2);       // 全局函数:void func(int i), i = 2;
    t1.func(2);    // void Test::func(int i), i = 2
    t1.func();     // void Test::func()
    
    return 0;
}

在这里插入图片描述

#include <stdio.h>
#include <string.h>

char* strcpy(char* buf, const char* str, unsigned int n)
{
    return strncpy(buf, str, n);
}

int main()
{
    const char* s = "D.T.Software";
    char buf[8] = {0};
    
    //strcpy(buf, s);
    strcpy(buf, s, sizeof(buf)-1);
    
    printf("%s\n", buf);
    
    return 0;
}

操作符重载:operator

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

type operator sign(const type& obj1, const type& obj2)//sign = + - * /
{
	type ret;
	
	return ret;
}

在这里插入图片描述

class type
{
	public:
	type operator sign(const type& obj)//sign = + - * /
	{
		type ret;
		
		return ret;
	}
};

在这里插入图片描述
代码案例:
成员函数和全局函数都可以实现操作符的重载

#include <stdio.h>

class Complex 
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    
    int getA()
    {
        return a;
    }
    
    int getB()
    {
        return b;
    }
    
    Complex operator + (const Complex& p)//成员函数
    {
        Complex ret;
        printf("Complex operator + (const Complex& p)\n");
        ret.a = this->a + p.a;
        ret.b = this->b + p.b;
        
        return ret;
    }
    
    friend Complex operator + (const Complex& p1, const Complex& p2);
};

Complex operator + (const Complex& p1, const Complex& p2)//全局函数
{
    Complex ret;
    printf("Complex operator + (const Complex& p1, const Complex& p2)\n");
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;
    
    return ret;
}

int main()
{

    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2; // c1.operator + (c2)
    
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    
    return 0;
}

完善复数类

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码案例

(1)complex.h-声明

#ifndef _COMPLEX_H_
#define _COMPLEX_H_

class Complex
{
    double a;//实部
    double b;//虚部
public:
    Complex(double a = 0, double b = 0);//构造函数:只能再函数声明的地方使用函数参数默认值
    double getA();//用于获取实部
    double getB();//用于获取虚部
    double getModulus();//求模函数
    
    //操作符重载函数
    Complex operator + (const Complex& c);//'+'
    Complex operator - (const Complex& c);//'-'
    Complex operator * (const Complex& c);//'*'
    Complex operator / (const Complex& c);//'/'
    //比较运算符重载函数
    bool operator == (const Complex& c);
    bool operator != (const Complex& c);
    //赋值操作符重载函数,只能重载为成员函数
    Complex& operator = (const Complex& c);
};

#endif

(2)complex.cpp-功能实现

#include "Complex.h"
#include "math.h"

Complex::Complex(double a, double b)//构造函数
{
    this->a = a;
    this->b = b;
}

double Complex::getA()
{
    return a;
}

double Complex::getB()
{
    return b;
}

double Complex::getModulus()//求模函数
{
    return sqrt(a * a + b * b);//sqrt(a^2 + b^2)
}

Complex Complex::operator + (const Complex& c)
{
    double na = a + c.a;
    double nb = b + c.b;
    Complex ret(na, nb);
    
    return ret;
}
//定义 = - * / 运算符重载函数
Complex Complex::operator - (const Complex& c)
{
    double na = a - c.a;
    double nb = b - c.b;
    Complex ret(na, nb);
    
    return ret;
}

Complex Complex::operator * (const Complex& c)
{
    double na = a * c.a - b * c.b;
    double nb = a * c.b + b * c.a;
    Complex ret(na, nb);
    
    return ret;
}

Complex Complex::operator / (const Complex& c)
{
    double cm = c.a * c.a + c.b * c.b;
    double na = (a * c.a + b * c.b) / cm;
    double nb = (b * c.a - a * c.b) / cm;
    Complex ret(na, nb);
    
    return ret;
}
//比较操作符重载函数    
bool Complex::operator == (const Complex& c)
{
    return (a == c.a) && (b == c.b);
}

bool Complex::operator != (const Complex& c)
{
    return !(*this == c);
}
//赋值操作符重载函数,只能作为重载成员函数       
Complex& Complex::operator = (const Complex& c)
{
    if( this != &c )//判断两个操作数是不是同一个对象
    {
        a = c.a;
        b = c.b;
    }
    
    return *this;//将当前对象返回
}

(3)main.c-应用

#include <stdio.h>
#include "Complex.h"

int main()
{
    Complex c1(1, 2);
    Complex c2(3, 6);
    Complex c3 = c2 - c1;
    Complex c4 = c1 * c3;
    Complex c5 = c2 / c1;
    
    printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
    printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());
    printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());
    
    Complex c6(2, 4);
    
    printf("c3 == c6 : %d\n", c3 == c6);
    printf("c3 != c4 : %d\n", c3 != c4);
    
    (c3 = c2) = c1;
    
    printf("c1.a = %f, c1.b = %f\n", c1.getA(), c1.getB());
    printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB());
    printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
    
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值