work day7

第七章  运算符重载

一、填空题

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

}

___ friend std::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 {

public:

    Complex(double real = 0.0, double imaginary = 0.0) {

        this->real = real;

        this->imaginary = imaginary;

    }

    Complex operator+(const Complex& other) const {

        double resultReal = real + other.real;

        double resultImaginary = imaginary + other.imaginary;

        return Complex(resultReal, resultImaginary);

    }

    Complex operator-(const Complex& other) const {

        double resultReal = real - other.real;

        double resultImaginary = imaginary - other.imaginary;

        return Complex(resultReal, resultImaginary);

    }

    friend Complex operator+(double num, const Complex& complex) {

        double resultReal = num + complex.real;

        double resultImaginary = complex.imaginary;

        return Complex(resultReal, resultImaginary);

    }

    friend Complex operator+(const Complex& complex, double num) {

        return num + complex;

    }

    friend Complex operator-(double num, const Complex& complex) {

        double resultReal = num - complex.real;

        double resultImaginary = -complex.imaginary;

        return Complex(resultReal, resultImaginary);

    }

    friend Complex operator-(const Complex& complex, double num) {

        return -num + complex;

    }

    void print() const {

        cout << real << " + " << imaginary << "i" << endl;

    }

private:

    double real;

    double imaginary;

};

int main() {

    Complex a(2, 5);

    Complex b(7, 8);

    Complex c(0, 0);

    c = a + b;

    c.print();

    c = 4.1 + a;

    c.print();

    c = b + 5.6;

    c.print();

    return 0;

}

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

#include <iostream>

using namespace std;

class Time {

public:

    Time(int h = 0, int m = 0, int s = 0) {

        hours = h;

        minutes = m;

        seconds = s;

    }

    void setTime(int h, int m, int s) {

        hours = h;

        minutes = m;

        seconds = s;

    }

    void getTime(int& h, int& m, int& s) const {

        h = hours;

        m = minutes;

        s = seconds;

    }

    void printTime() const {

        cout << hours << ":" << minutes << ":" << seconds << endl;

    }

    Time operator+(const Time& other) const {

        int totalSeconds = seconds + other.seconds;

        int carryMinutes = totalSeconds / 60;

        int remainingSeconds = totalSeconds % 60;

        int totalMinutes = minutes + other.minutes + carryMinutes;

        int carryHours = totalMinutes / 60;

        int remainingMinutes = totalMinutes % 60;

        int totalHours = hours + other.hours + carryHours;

        return Time(totalHours, remainingMinutes, remainingSeconds);

    }

    Time operator-(const Time& other) const {

        int totalSeconds = seconds - other.seconds;

        int borrowMinutes = 0;

        if (totalSeconds < 0) {

            totalSeconds += 60;

            borrowMinutes = 1;

        }

        int totalMinutes = minutes - other.minutes - borrowMinutes;

        int borrowHours = 0;

        if (totalMinutes < 0) {

            totalMinutes += 60;

            borrowHours = 1;

        }

        int totalHours = hours - other.hours - borrowHours;

        return Time(totalHours, totalMinutes, totalSeconds);

    }

private:

    int hours;

    int minutes;

    int seconds;

};

int main() {

    Time t1(10, 30, 45);

    Time t2(2, 15, 20);

    Time sum = t1 + t2;

    Time diff = t1 - t2;

    cout << "t1: ";

    t1.printTime();

    cout << "t2: ";

    t2.printTime();

    cout << "Sum: ";

    sum.printTime();

    cout << "Difference: ";

    diff.printTime();

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

    }
 

相关推荐

  1. CTF <span style='color:red;'>7</span>

    CTF 7

    2024-02-08 15:06:03      36 阅读
  2. ARMday<span style='color:red;'>7</span>

    ARMday7

    2024-02-08 15:06:03      33 阅读
  3. DevOps(7)

    2024-02-08 15:06:03       33 阅读
  4. HCIP-<span style='color:red;'>7</span>

    HCIP-7

    2024-02-08 15:06:03      34 阅读
  5. ARMday<span style='color:red;'>7</span>

    ARMday7

    2024-02-08 15:06:03      20 阅读
  6. <span style='color:red;'>7</span>.string

    7.string

    2024-02-08 15:06:03      15 阅读
  7. MySQL(7

    2024-02-08 15:06:03       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-08 15:06:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-08 15:06:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-08 15:06:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-08 15:06:03       18 阅读

热门阅读

  1. 【mybatis自动治愈插件】

    2024-02-08 15:06:03       31 阅读
  2. LeetCode 二叉树/n叉树的解题思路

    2024-02-08 15:06:03       37 阅读
  3. Mockito测试框架中的方法详解

    2024-02-08 15:06:03       32 阅读
  4. C语言中大小写字母转换详解

    2024-02-08 15:06:03       34 阅读
  5. 【六】CocosCreator-CCObject.js源码分析

    2024-02-08 15:06:03       30 阅读
  6. 最全软件系统架构演变!

    2024-02-08 15:06:03       35 阅读
  7. 精通Python中的正则表达式

    2024-02-08 15:06:03       33 阅读
  8. 贪心算法入门题(算法村第十七关青铜挑战)

    2024-02-08 15:06:03       32 阅读