2.7作业

一、填空题

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

}

_Magic______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>

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"iostream"
using namespace std;
class Time
{
public:
	Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}
	void SetTime(int,int,int);
	Time operator + (Time &a);
	Time operator - (Time &a);
	friend istream & operator >>(istream&,Time&);
	friend ostream & operator <<(ostream&,Time&);
private:
	int hour,minute,second;
};
void Time::SetTime(int h,int m,int s)
{
	hour=h;
	minute=m;
	second=s;
}
Time Time::operator + (Time &a)
{
	return Time(hour+a.hour,minute+a.minute,second+a.second);
}
Time Time::operator - (Time &a)
{
	return Time(hour-a.hour,minute-a.minute,second-a.second);
}
istream & operator >>(istream&is,Time&a)
{
	is>>a.hour>>a.minute>>a.second;
	return is;
}
ostream & operator <<(ostream&os,Time&b)
{
	os<<b.hour<<'/'<<b.minute<<'/'<<b.second<<endl;
	return os;
} 
int main()
{
	Time a,b,c;
	cout<<"请输入A的时间:"<<endl;
	cin>>a;
	cout<<"请输入B的时间:"<<endl;
	cin>>b;
	cout<<"A的时间为:";
	cout<<a;
	cout<<"B的时间为:";
	cout<<b;
	cout<<"c=a+b=";
	c=a+b;
	cout<<c;
	cout<<endl<<"c=a-b=";
	c=a-b;
	cout<<c;
	return 0;
}

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

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

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

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

相关推荐

最近更新

  1. TCP协议是安全的吗?

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

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

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

    2024-02-08 01:54:03       18 阅读

热门阅读

  1. Lua编译与运行

    2024-02-08 01:54:03       31 阅读
  2. 【算法题】92. 反转链表 II

    2024-02-08 01:54:03       27 阅读
  3. 分支解决冲突 & 分支管理策略 git merge命令详解

    2024-02-08 01:54:03       32 阅读
  4. 【Git】三棵“树”介绍

    2024-02-08 01:54:03       35 阅读
  5. (39)统计位数为偶数的数字

    2024-02-08 01:54:03       36 阅读
  6. work day7

    2024-02-08 01:54:03       28 阅读
  7. PyTorch自动微分模块torch.autograd的详细介绍

    2024-02-08 01:54:03       32 阅读