2024/2/7

第七章  运算符重载

一、填空题

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

#include 

#include 

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));

}

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

{ stream<

return stream;

}

};

int main()

{Magic ma;

cout<

Magic(-8)<

}

二、编程题

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

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

  c=a+b;

  c=4.1+a;

  c=b+5.6;

#include 
 
class Complex {
private:
    double real;
    double imag;
 
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
 
    Complex operator+(const Complex& other) const {
        return Complex(real + other.real, imag + other.imag);
    }
 
    Complex operator-(const Complex& other) const {
        return Complex(real - other.real, imag - other.imag);
    }
 
    Complex operator+(double num) const {
        return Complex(real + num, imag);
    }
 
    Complex operator-(double num) const {
        return Complex(real - num, imag);
    }
 
    friend Complex operator+(double num, const Complex& c) {
        return Complex(num + c.real, c.imag);
    }
 
    friend Complex operator-(double num, const Complex& c) {
        return Complex(num - c.real, -c.imag);
    }
 
    void display() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};
 
int main() {
    Complex a(2, 5), b(7, 8), c(0, 0);
    
    c = a + b;
    c.display(); // 输出:(9, 13)
    
    c = 4.1 + a;
    c.display(); // 输出:(6.1, 5)
    
    c = b + 5.6;
    c.display(); // 输出:(12.6, 8)
    
    return 0;
}

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

#include 
#include 
using namespace std;
class Time
{
private:
    int hours;  //小时
    int minutes;  //分钟
    int seconds;  //秒
public:
    //构造函数
    Time(int h,int m,int s):hours(h),minutes(m),seconds(s){}
    //加法操作
    Time operator+(const Time &other)const
    {
        int total_seconds=seconds+other.seconds+60*(minutes+other.minutes)+3600*(hours+other.hours);
        int new_seconds=total_seconds%60;
        int new_minutes=(total_seconds/60)%60;
        int new_hours=total_seconds/3600;
        return Time(new_hours,new_minutes,new_seconds);
    }
    //减法操作
    Time operator-(const Time &other)const
    {
        int total_seconds=seconds-other.seconds+60*(minutes-other.minutes)+3600*(hours-other.hours);
        if(total_seconds<0)
        {
            total_seconds+=3600*24;//如果小于0,借一天
        }
        int new_seconds=total_seconds%60;
        int new_minutes=(total_seconds/60)%60;
        int new_hours=total_seconds/3600;
        return Time(new_hours,new_minutes,new_seconds);
    }
    //读取时间
    void setTime(int h,int m,int s)
    {
        hours=h;
        minutes=m;
        seconds=s;
    }
    //输出时间
    void show()const
    {
        cout << hours << ":" << minutes << ":" << seconds << endl;
    }
};
int main()
{
    Time t1(14,25,13);
    Time t2(3,12,23);
    //加法
    Time t3=t1+t2;
    cout << "t1+t2=";
    t3.show();
    //减法
    Time t4=t1-t2;
    cout << "t1-t2=";
    t4.show();
    //读取时间
    t1.setTime(15,30,0);
    cout << "Set t1 to ";
    t1.show();
    return 0;
}

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

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

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

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

你可能感兴趣的:(c++)