2月7号寒假作业

本文介绍了C++中的运算符重载,包括类Magic中自定义加法运算符、复数类的加减运算、时间类的时间计算以及货币类与double类型相乘的运算符实现。

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

第七章  运算符重载

一、填空题

1、在下列程序的空格处填上适当的字句,使输出为:0,2,10。

#include <iostream>

#include <math.h>

class Magic

{double x;

public:

Magic(double d=0.00):x(fabs(d))

{}

Magic operator+(_const Magic &c_____)

{

return Magic(sqrt(x*x+c.x*c.x));

}

_ostream operator<<(ostream & stream,const  Magic & c)

{ stream<<c.x;

return stream;

}

};

int main()

{Magic ma;

cout<<ma<<", "<<Magic(2)<<", "<<ma+Magic(-6)+

Magic(-8)<<endl;

}

二、编程题

1、 定义复数类的加法与减法,使之能够执行下列运算:

  Complex a(2,5),b(7,8),c(0,0);

  c=a+b;

  c=4.1+a;

  c=b+5.6;

#include <iostream>

using namespace std;

class Complex
{
    friend class Constant;
private:
    double x;
    double y;
public:
    Complex(double x=0,double y=0):x(x),y(y)
    {}
    const Complex operator+(const Complex &c) const
    {
        Complex temp;
        temp.x=x+c.x;
        temp.y=y+c.y;
        return temp;
    }
    const Complex operator+(const double x) const
    {
        Complex temp;
        temp.x=this->x+x;
        temp.y=y;
        return temp;
    }
    void show()
    {
        cout << x << "   " << y << endl;
    }
};

class Constant
{
private:
    double x;
public:
    Constant(double x=0):x(x)
    {}
    const Complex operator+(const Complex &c) const
    {
        Complex temp;
        temp.x=x+c.x;
        temp.y=x+c.y;
        return temp;
    }
};

int main()
{
    Complex a(2,5);
    Complex b(7,8);
    Complex c;
    Constant d(4.1);

    c=a+b;
    c.show();

    c=d+a;
    c.show();

    c=b+5.6;
    c.show();

    return 0;
}

2、 编写一个时间类,实现时间的加、减、读和输出。

#include <iostream>

using namespace std;

class Time
{
private:
    int hour;
    int min;
    int sec;
public:
    Time(int hour=0,int min=0,int sec=0):hour(hour),min(min),sec(sec)
    {}
    const Time operator+(const Time &t) const
    {
        Time temp;
        int flag=0;
        temp.sec=sec+t.sec;
        if(temp.sec>=60)
        {
            flag=1;
            temp.sec-=60;
        }

        temp.min=min+t.min;
        if(flag==1)
        {
            temp.min++;
        }
        if(temp.min>=60)
        {
            flag=2;
            temp.min-=60;
        }

        temp.hour=hour+t.hour;
        if(flag==2)
        {
            temp.hour++;
        }
        if(temp.hour>=24)
        {
            temp.hour-=24;
        }

        return temp;
    }

    const Time operator-(const Time &t) const
    {
        Time temp;
        int flag=0;
        temp.sec=sec-t.sec;
        if(temp.sec<0)
        {
            flag=1;
            temp.sec+=60;
        }

        temp.min=min-t.min;
        if(flag==1)
        {
            temp.min--;
        }
        if(temp.min<0)
        {
            flag=2;
            temp.min+=60;
        }

        temp.hour=hour-t.hour;
        if(flag==2)
        {
            temp.hour--;
        }
        if(temp.hour<0)
        {
            temp.hour+=24;
        }
        return temp;
    }

    void show()
    {
        cout << hour << " : " << min << " : " << sec << endl;
    }
};

int main()
{
    Time t1(22,51,33);
    Time t2(15,44,55);
    Time t3=t1+t2;
    t3.show();

    Time t4=t2-t1;
    t4.show();


    return 0;
}

3、 增加操作符,以允许人民币与double型数相乘。

  friend money operator*(const money&,double);

  friend money operator*(double,const money&);

  注意:两个money对象不允许相乘。

#include <iostream>
 
using namespace std;
 
class Money {
 
private:
 
    double amount;
 
public:
 
    Money(double amt = 0.0) : amount(amt) {}
 
    friend Money operator*(const Money& money, double num) {
 
        return Money(money.amount * num);
    }
 
    friend Money operator*(double num, const Money& money) {
 
        return Money(money.amount * num);
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值