结构体运算符重载

本文介绍了如何在C++中使用结构体实现多种运算符重载,包括输入输出、赋值、加减乘操作,并演示了自定义输出函数的用法。通过实例代码展示了结构体在不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

仅供参考,这个是转载的,对于一些重载,比如说重载=操作符,对原作者结构认同,但是对于原作者的重载方法不认同,所以已经修改,这个如果希望看到原版,请参考本文最后的原文链接

1、定义结构体

<span style="font-size:14px;">struct Currency
{
int Dollar;
int Cents;
char *name;
 }
</span>


2、重载IO输出操作,在结构体内部将输入操作的重载定义为友元函数重载
<span style="font-size:14px;">friend ostream &operator<<(ostream &out,Currency value);</span>



在结构体外部进行具体定义
<span style="font-size:14px;">ostream& operator<<(ostream &out,Currency value)
{
out<<"The dollar = "<<value.Dollar<<" and The Cents = "<<value.Cents<<endl;
return out;
}</span>



3、重载结构体的“=”操作符(在结构体内部)
<span style="font-size:14px;">Currency&operator=(struct Currency &b)
    {
        if (this!=&b)
        {
            delete name;
            name=(char *)malloc(10);
            strcpy(name,b.name);
        }
        return *this;
    }
</span>


4、重载结构体的“+”操作符(在结构体内部)

<span style="font-size:14px;">Currency& operator+(Currency& value)
{
Dollar += value.Dollar;
Cents += value.Cents;
return *this;
}

</span>


5、重载结构体的"-"操作符(在结构体内部)
Currency &operator-(Currency& value)
{
Dollar = Dollar - value.Dollar;
Cents = Cents - value.Cents;
return *this;
}



6、重载结构体的“*”操作符(在结构体内部)
Currency& operator*(Currency& value)
{
Dollar *= value.Dollar;
Cents *= value.Cents;
return *this;
}



7、定义函数模板格式的输出函数
template <typename T>
void DisplayValue(T value)
{
cout<<value<<endl;
}



8、进行运行测试。。。
Currency c1;
c1.Dollar = 10;
c1.Cents = 5;
DisplayValue(c1);
Currency c2,c3;
c2 = c1;
c3= c1+c2;
DisplayValue(c3);



附上完整程序代码。。。
#include "stdafx.h"
#include <iostream>
using namespace std;

template <typename T>
void DisplayValue(T value)
{
cout<<value<<endl;
}

struct Currency
{
int Dollar;
int Cents;

Currency& operator=(Currency& value)
{
Dollar = value.Dollar;
Cents = value.Cents;
return *this;
}
Currency& operator+(Currency& value)
{
Dollar += value.Dollar;
Cents += value.Cents;
return *this;
}
Currency &operator-(Currency& value)
{
Dollar = Dollar - value.Dollar;
Cents = Cents - value.Cents;
return *this;
}
Currency& operator*(Currency& value)
{
Dollar *= value.Dollar;
Cents *= value.Cents;
return *this;
}
friend ostream &operator<<(ostream &out,Currency value);
};

ostream& operator<<(ostream &out,Currency value)
{
out<<"The dollar = "<<value.Dollar<<" and The Cents = "<<value.Cents<<endl;
return out;
}
int _tmain(int argc, _TCHAR* argv[])
{

Currency c1;
c1.Dollar = 10;
c1.Cents = 5;
DisplayValue(c1);
Currency c2,c3;
c2 = c1;
c3= c1+c2;
DisplayValue(c3);
system("pause");
return 0;
}

最后在原文的基础上,加上一个重载运算符的例子


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

struct stu
{
    char *name, gender;
    int score;
    /*重载赋值运算符开始*/
    stu&operator=(struct stu &b)
    {
        if (this!=&b)
        {
            delete name;
            name=(char *)malloc(10);
            strcpy(name,b.name);
        }
        return *this;
    }
    /*重载赋值运算符结束*/
};

int main()
{
    struct stu a = {NULL, 'm', 290}, b;
    a.name = (char *)malloc(10);
    strcpy(a.name, "zhao");
    
    b = a;
    b.gender = 'f';
    b.score = 350;
    strcpy(b.name, "qian");
    
    printf("%s, %c, %d\n", a.name, a.gender, a.score);
    printf("%s, %c, %d\n", b.name, b.gender, b.score);
}




原文地址:

作者:imFolish  
出处:http://xielechuan.cnblogs.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值