第七章 运算符重载

判断题

1.对每个可重载的运算符来讲,它既可以重载为友元函数,又可以重载为成员函数,还可以重载为非成员函数。(F)

2.对单目运算符重载为友元函数时,可以说明一个形参。而重载为成员函数时,不能显式说明形参。(T)

3.重载operator+时,返回值的类型应当与形参类型一致。 比如以下程序中,operator+的返回值类型有错:(F)

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

4.In C++, only existing operators can be overloaded.(T)
在C++中,只有现有的运算符可以重载。

2.对单目运算符重载为友元函数时,可以说明一个形参。而重载为成员函数时,不能显式说明形参。(T)

单选题

1.下列运算符中,( )运算符不能重载。(C)

A. &&
B. []
C. ::
D. <<

// C++中五个不能重载的运算符:
① . ② *(指针)③:: ④?: ⑤sizeof

2.下列关于运算符重载的描述中,( )是正确的。(D)

A.运算符重载可以改变操作数的个数
B.运算符重载可以改变优先级
C.运算符重载可以改变结合性
D.运算符重载不可以改变语法结构

4.能用友元函数重载的运算符是()。(A)

A.+
B.=
C.[]
D.->
//不能用友元函数重载的运算符:
① = ② () ③-> ④ []

3.为了能出现在赋值表达式的左右两边,重载的"[]"运算符应定义为:(B)

A. A operator [ ] (int);
B. A& operator [ ] (int);
C.const A operator [ ] (int);
D.以上答案都不对

函数题

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 Complex(realPart,imgPart);
}

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++;
	return Clock(op.Hour,op.Minute,op.Second);
}
Clock operator++(Clock& op,int) // 后置
{
	int tmpS;
	tmpS = op.Second;
	op.Second++;
	return Clock(op.Hour,op.Minute,tmpS);
}

3.重载±运算符
请根据程序的输出结果,重载类A的+和-运算符。
类和函数接口定义:

class A {
public:
    A(int x = 0, int y = 0) : x(x), y(y) {}
    void show() const;
    A operator+(A& a);    //重载+运算符
    A operator-(A& a);    //重载-运算符
private:
    int x, y;
};

裁判测试程序样例:

#include <iostream>
using namespace std;

class A {
public:
    A(int x = 0, int y = 0) : x(x), y(y) {}
    void show() const;
    A operator+(A& a);
    A operator-(A& a);
private:
    int x, y;
};

void A::show() const {
    cout << "(x,y) = " << "(" << x << "," << y << ")" << endl;
}

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

int main() {
    A a1(1, 2);
    A a2(4, 5);
    A a;
    cout << "a1:";    a1.show();
    cout << "a2:";    a2.show();
    a = a1 + a2;
    cout << "a:";    a.show();
    a = a1 - a2;
    cout << "a:";    a.show();
    return 0;
}

输入样例:

本题无输入。

输出样例:

a1:(x,y) = (1,2)
a2:(x,y) = (4,5)
a:(x,y) = (5,7)
a:(x,y) = (-3,-3)
// 注意细节

A A::operator+(A& a)
{
	int x1,y1;
	x1 =x + a.x;
	y1 =y + a.y;
	return A(x1,y1);
}
A A::operator-(A& a)
{
	int x1,y1;
	x1 =x - a.x;
	y1 =y - a.y;
	return A(x1,y1);
}

1.对学生对象按照成绩升序排序
下面这段代码要实现对学生对象按照成绩升序排序。 仔细阅读代码,要求实现编程实现输出运算符“<<”和小于“<”运算符,使本程序能完成预定的排序功能。

裁判测试程序样例:

#include <iostream>
#include <string>
#include <list>
using namespace std;

class Student {
   string name;
   char sex;
   int score;
   string grade;

public:
   Student(string name, char sex, int score, string grade);
   friend ostream &operator<< (ostream& os, Student st) ;
   friend bool operator<(Student &st1, Student &st2);    
};
//你提交的代码将被嵌入到这里

Student::Student(string name, char sex, int score, string grade) {
   this->name = name;
   this->sex = sex;
   this->score = score;
   this->grade = grade;
}

int main() {
   list<Student> st;
   string name, grade;
   char sex;      int score;

   for(int i = 0; i < 5; i++) {
      cin>>name;      cin>>sex;
      cin>>score;       cin>>grade;
      st.push_back(Student(name, sex, score, grade));
   }

   st.sort();

   list<Student>::iterator p = st.begin();
   while(p != st.end()) {
      cout<<*p;
      p++;
   }
   return 0;
}

输入样例:

Bill M 86 JK1501
David M 98 JK1502
Mary F 78 JK1503
Adam M 83 JK1504
Rose F 96 JK1505

输出样例:

Mary F 78 JK1503
Adam M 83 JK1504
Bill M 86 JK1501
Rose F 96 JK1505
David M 98 JK1502

//

ostream &operator<< (ostream& os, Student st)
{
    os << st.name << " " << st.sex << " " << st.score << " " << st.grade << endl;
}
bool operator < (Student &st1, Student &st2)
{
     if(st1.score < st2.score)
        return true;
     return false;
}

2.重载下标运算符[ ]
这段程序实现了安全数组。 请认真阅读程序并补全程序使之能正确执行。

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:
#include <iostream>
#include <cstdlib>
using namespace std;
const int SIZE = 3;

class atype {
    int a[SIZE];
public:
   atype( ) {
       register int i;
       for(i=0; i<SIZE; i++) a[i] = i;
  }
  int &operator[](int i);
};
//你提交的代码将被嵌入到这里

int main( )
{
   atype ob;
   cin >> ob[1];  
   ob[2] = ob[1];  // 下标运算符[]出现在赋值运算符的左边和右边 
   cout << ob[2];  
   ob[3] = 44; // 产生运行时错误,下标3超出了数组边界
   return 0;
}

输入样例:

98

输出样例:

98
Index value of 3 is out-of-bounds.
int &atype :: operator[](int i) {
    if (i < 0 || i >= SIZE) {
        cout << endl;
        cout << "Index value of 3 is out-of-bounds.";
        exit(0);
    }
    return a[i];
}

3.复数类重载加法、减法和乘法运算符

复数类定义:

在这里描述复数类定义。具体如下:
class complex {
    public:
        complex(float r=0,float i=0);                   // 构造函数
        complex operator+(const complex &op2) const;    //重载运算符 +
        complex operator-(const complex &op2) const;    //重载运算符 -
        complex operator*(const complex &op2) const;    //重载运算符 *
        void display() const;                           // 按数学写法输出复数
    private:
        float real;
        float imag;
};

裁判测试程序样例:

在这里给出复数类测试的例子。例如:
#include <iostream>
using namespace std;
class complex {
    public:
        complex(float r=0,float i=0);                   // 构造函数
        complex operator+(const complex &op2) const;    //重载运算符 +
        complex operator-(const complex &op2) const;    //重载运算符 -
        complex operator*(const complex &op2) const;    //重载运算符 *
        void display() const;                           // 按数学写法输出复数
    private:
        float real;
        float imag;
};


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


void complex::display() const {
    if(real&&imag)
        if(imag==1||imag==-1)
            cout<<real<<(imag>0?"+":"-")<<"i"<<endl;
        else
            cout<<real<<(imag>0?"+":"")<<imag<<"i"<<endl;
    else if(real)
        cout<<real<<endl;
    else if (imag)
        if(imag==1||imag==-1)
            cout<<(imag>0?"":"-")<<"i"<<endl;
        else
            cout<<imag<<"i"<<endl;
    else
        cout<<0<<endl;
}

int main() {
    double real,imag;
    complex c,d,e;

    cin>>real>>imag;
    complex c1(real,imag);
    cin>>real>>imag;
    complex c2(real,imag);

    c=c1+c2;
    d=c1-c2;
    e=c1*c2;
    c.display() ;
    d.display() ;
    e.display();

    return 0;
}

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

2 3
-4 -5

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

-2-2i
6+8i
7-22i

//注意构造函数
complex::complex(float r,float i)
{
	real = r;
	imag = i;
}

complex complex::operator+(const complex &op2) const    //重载运算符 +
{
	float real1=real+op2.real;
	float imag1=imag+op2.imag;
	return complex(real1,imag1);
}

complex complex::operator-(const complex &op2) const    //重载运算符 -
{
	float real1=real-op2.real;
	float imag1=imag-op2.imag;
	return complex(real1,imag1);
}

complex complex::operator*(const complex &op2) const    //重载运算符 *
{
	float real1=real*op2.real-imag*op2.imag;
	float imag1=real*op2.imag+imag*op2.real;
	return complex(real1,imag1);
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值