C++运算符重载

1-1 对每个可重载的运算符来讲,它既可以重载为友元函数,又可以重载为成员函数,还可以重载为非成员函数。 ( F )
1-2 对单目运算符重载为友元函数时,可以说明一个形参。而重载为成员函数时,不能显式说明形参。 ( T )
1-3 重载operator+时,返回值的类型应当与形参类型一致。 比如以下程序中,operator+的返回值类型有错: ( F )

class A {
int x;
public:
A(int t=0):x(t){ }
int operator+(const A& a1){ return x+a1.x;  }
};        

1-4 In C++, only existing operators can be overloaded. ( T )

2-1 下列运算符中,( )运算符不能重载。 ( C )
A. &&
B. [ ]
C. ::
D. <<
2-2下列关于运算符重载的描述中,( )是正确的。 ( D )
A. 运算符重载可以改变操作数的个数
B. 运算符重载可以改变优先级
C. 运算符重载可以改变结合性
D. 运算符重载不可以改变语法结构

3-1 A binary operator can be defined by either a non-static member function taking one argument or a nonmember function taking two arguments.
3-2 A unary operator, whether prefix or postfix, can be defined by either a non-static member function taking no arguments or a nonmember function taking one argument.

4-1 使用成员函数重载复数类的运算符+
类Complex声明了一个复数类,有两个数据成员realPart(代表复数的实部)和imgPart(代表复数的虚部),并定义了成员函数实现了重载运算符“+”以实现两个复数对象的相加操作。成员函数Show用来输出复数的实部和虚部。请完成对运算符“+”的重载操作。

函数接口定义:

Complex& Complex::operator+(Complex& com);

参数com为复数类Complex的对象的引用,函数的返回值为当前对象与com对象相加后的值。

裁判测试程序样例:

#include<iostream>
using namespace std;

class Complex {
public:
    Complex(double realPart = 0, double imgPart = 0) {
        this->realPart = realPart;
        this->imgPart = imgPart;
    }
    Complex& operator+(Complex& com);
    void Show() {
        cout << realPart << " " << imgPart << endl;
    }
private:
    double realPart, imgPart;
};
int main() {
    int r1, i1;			//第1个复数对象的实部和虚部
    int r2, i2;			//第1个复数对象的实部和虚部
    cin >> r1 >> i1;
    cin >> r2 >> i2;
    Complex c1(r1, i1);	//构造第1个复数对象c1
    Complex c2(r2, i2);	//构造第2个复数对象c2
    c1 = c1 + c2;
    c1.Show();

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:
3 4
10 20

输出样例:
13 24

Complex  &Complex :: operator+(Complex& com) 
{
    realPart += com.realPart;
    imgPart += com.imgPart;
    return *this;
}

4-2 单目运算符重载(时钟类)
本题已给出时钟类及其成员函数实现,要求补充完整运算符++重载函数(前置和后置),使之能够实现时钟对象自增1秒。

时钟类定义如下:

class Clock {
	public:
		Clock(int NewH=0, int NewM=0, int NewS=0);
		void ShowTime();
		friend Clock operator++(Clock& op);         //前置单目运算符重载
		friend Clock operator++(Clock& op,int);     //后置单目运算符重载
	private:
		int Hour, Minute, Second;
};

裁判测试程序样例:

#include<iostream>
using namespace std;

class Clock {
	public:
		Clock(int NewH=0, int NewM=0, int NewS=0);
		void ShowTime();
		friend Clock operator++(Clock& op);         //前置单目运算符重载
		friend Clock operator++(Clock& op,int);     //后置单目运算符重载
	private:
		int Hour, Minute, Second;
};

Clock::Clock(int NewH, int NewM, int NewS) {
	Hour=NewH;
	Minute=NewM;
	Second=NewS;
}
void Clock::ShowTime() {
	cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}

/*---------请在这里填写答案-----------*/

int main() {
	int h, m, s;
	cin>>h>>m>>s;
	Clock a(h,m,s);

	(++a).ShowTime();
	(a++).ShowTime();
	a.ShowTime();

	return 0;
}

输入样例:
在这里给出一组输入。例如:

10 10 10

输出样例:
在这里给出相应的输出。例如:

10:10:11
10:10:11
10:10:12

Clock operator ++(Clock &op) 
{
    op.Second++;
    if (op.Second >= 60) 
    {
        op.Second = op.Second - 60;
        op.Minute++;
        if (op.Minute >= 60) 
        {
            op.Minute = op.Minute - 60;
            op.Hour++;
            op.Hour = op.Hour % 24;
        }
    }
    return op;
}
Clock operator ++(Clock &op, int) 
{
    Clock OP = op;
    op.Second++;
    if (op.Second >= 60) 
    {
        op.Second = op.Second - 60;
        op.Minute++;
        if (op.Minute >= 60) 
        {
            op.Minute = op.Minute - 60;
            op.Hour++;
            op.Hour = op.Hour % 24;
        }
    }
    return OP;
}
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wings(hha)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值