1、运算符重载 复习题1. 重载赋值操作符时,应声明为( ) A. 友元函数 B. 虚函数 C. 成员函数 D. 多态函数2.关于重载的描述,正确的是( )A.函数重载只能在成员函数之间进行B.函数重载只能在全局函数之间进行C.函数重载可以在基类和派生类之间进行D.函数重载必须要求同名的函数的形参类型和个数都完全一致,返回值类型无所谓3下列运算符中不能重载的是 ( )。A (域运算符 ) B.+ (正) C.+ (自增) D.*(指针)4. 派生类的对象对它的基类成员中( )A. 公有继承的公有成员是可以访问的 B. 公有继承的私有成员是可以访问的C. 公有继承的保护成员是可以访问的 D. 私有继
2、承的公有成员是可以访问的5 不能重载的运算符是( )A. ?: B. C. new D. imag=i;Complex operator +(Complex);friend Complex operator -(Complex,Complex);private:double real,imag;Complex Complex: operator +(Complex c)return *this;operator -(Complex c1,Complex c2)return Complex( _);33 this-real+=c.real,this-imag+=c.imag; Complex c
3、1.real+c2.real,c1.imag+c2.imag 15设有类的定义:class Matrix /定义Matrix 类public:Matrix(); /默认构造函数friend Matrix operator+(Matrix /重载运算符“+”void input(); /输入数据函数void display(); /输出数据函数private:int mat23; ;实现两个矩阵 A 和 B(均为 2 行 3 列)的和。试写出成员函数 Matrix( )实现构造函数初始化 0 值,友员函数 Matrix operator+(Matrix iusing namespace std;
4、class Complexpublic:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator + (Complex void display();private:double real;double imag;Complex Complex:operator + (Complex c.real=real+c2.real;c.imag=imag+c2.imag;return c;void Complex:display()coutusing namespace std;class Compl
5、expublic:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator + (Complex void display();private:double real;double imag;Complex Complex:operator + (Complex c.real=real+c2.real;c.imag=imag+c2.imag;return c;void Complex:display()coutclass Coordpublic:Coord(int i=0,int j=0)x=
6、i;y=j;void Print()coutclass pointprivate:float x,y;public:point(float xx=0,float yy=0) x=xx,y=yy;point(pointpoint();bool operator=(point);bool operator!=(point);pointfloat getx()return x;float gety()return y;point:point(pointelseReturn 0;bool point:operator!=(point p)if( )Return 1;elseReturn 0;point
7、this-y+=p.gety();return void main()point p1(1,2),p2(3,4);p3(5,6);coutusing namespace std;int main() (1 分)void convert(int n);int number; (1 分)coutnumber; (1 分)cout#include class Stringpublic:String()p=NULL;String(char *str);friend bool operator(String void display();private:char *p;String:String(cha
8、r *str)p=str;void String:display()cout(String elsereturn false;void compare(String cout“;string2.display();else string1.display();coutusing namespace std;class Timepublic:Time()minute=0;sec=0;Time(int m,int s):minute(m),sec(s)Time operator+();void display()cout=60)sec-=60;+minute;return *this;int ma
9、in()Time time1(34,58);for (int i=1;ireal+=c.real,this-imag+=c.imag; Complex c1.real+c2.real,c1.imag+c2.imag 24仔细阅读程序,在题后写出其运行结果。#include using namespace std;class Timepublic:Time()minute=0;sec=0;Time(int m,int s):minute(m),sec(s)Time operator+();Time operator+(int);void display()cout=60)sec-=60;+min
10、ute;return *this;Time Time:operator+(int)Time temp(*this);sec+;if(sec=60)sec-=60;+minute;return temp;int main()Time time1(34,59),time2;coutusing namespace std;class Complexpublic:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i; (2 分)double get_real(); double get_imag(); (2 分)void disp
11、lay(); private:double real;double imag; (2 分);double Complex:get_real()return real; (2 分)double Complex:get_imag()return imag; (2 分)void Complex:display()cout“(“real“,“imag“i)“endl; (2 分)Complex operator + (Complex (2 分)int main()Complex c1(3,4),c2(5,-10),c3; (2 分)c3=c1+c2; (2 分)cout“c3=“;c3.display(); (2 分)return 0;