1. #include class T { public : T() { a = 0; b = 0; c = 0; } T( int i , int j , int k ) { a = i; b =j ; c = k; } void get( int &i , int &j , int &k ) { i = a; j = b; k = c; } T operator * ( T obj ); private: int a , b , c; }; T T::operator * ( T obj ) { T tempobj; tempobj.a = a * obj.a; tempobj.b = b * obj.b; tempobj.c = c * obj.c; return tempobj; } void main() { T obj1( 1,2,3 ), obj2( 5,5,5 ), obj3; int a , b , c; obj3 = obj1 * obj2; obj3.get( a, b, c ); cout << "( obj1 * obj2 ): \" << "a = " << a << '\' << "b = " << b<< '\' << "c = " << c << '\' << endl; ( obj2 * obj3 ).get( a, b, c ); cout << "( obj2 * obj3 ): \ " << "a = " << a << '\' << "b = " << b << '\' << "c = "<< c << '\' << endl; } 2. #include < iostream.h > class Vector { public: Vector(){ } Vector(int i,int j) { x = i ; y = j ; } friend Vector operator + ( Vector v1, Vector v2 ) { Vector tempVector ; tempVector.x = v1.x + v2.x ; tempVector.y = v1.y + v2.y ; return tempVector ; } void display() { cout << "( " << x << ", " << y << ") "<< endl ; } private: int x , y ; }; void main() { Vector v1( 1, 2 ), v2( 3, 4 ), v3 ; cout << "v1 = " ; v1.display() ; cout << "v2 = " ; v2.display() ; v3 = v1 + v2 ; cout << "v3 = v1 + v2 = " ; v3.display() ; } 6.2 思考题 1. 一个运算符重载函数被定义为成员函数或友员函数,从定义方式、解释方式和调用方式上有何区别?可能会出现什么问题?请用一个实例说明之。 2. 类类型对象之间,类类型和基本类型对象之间用什么函数进行类型转换?归纳进行类型转换的构造函数和类型转换函数的定义形式、调用形式和调用时机。 1. 一个运算符重载函数被定义为成员函数或友员函数,从定义方式、解释方式和调用方式上有何区别?可能会出现什么问题?请用一个实例说明之。 【答案】 以二元运算符为例。 Obj operator op(object); ObjL.operator op(ObjR) ObjL op ObjR 左操作数通过this指针指定,右操作数由参数传递 friend Obj operator op(Obj,Obj); operator op(ObjL,ObjR) ObjL op ObjR 操作数均由参数传递 (1) 运算符的左右操作数不同,须用友员函数重载; (2) 当运算符的操作需要修改类对象状态时,应用成员函数重载。 (3) 友员函数不能重载运算符 = () [] → 必须要用友员函数重载的运算符 >> << 程序略。 2. 类类型对象之间,类类型和基本类型对象之间用什么函数进行类型转换?归纳进行类型转换的构造函数和类型转换函数的定义形式、调用形式和调用时机。 【答案】 构造函数可以把基本类型、类类型数据转换成类类型数据;类类型转换函数可以在类类型和基本数据类型之间做数据转换。 自动类型转换时隐式调用 1.分别使用成员函数和友员函数编程序重载运算符“+”,使该运算符能实现两个字符串的连接。 答案 6.3-1 (1)使用成员函数 #include class string { public: string(){ *str = '\\0'; } string( char *pstr ) { strcpy( str,pstr ); } char *gets() { return str; } string operator+( string obj ); private: char str[100]; }; string string::operator+( string obj ) { strcat( str,obj.str ); return str; //或return *this } void main() { string obj1( "Visual" ),obj2( " C++" ),obj3; obj3 = obj1 + obj2; cout << obj3.gets() << endl; } (2)使用友员函数 #include #include class string { public: string(){ *str= '\\0'; } string( char *pstr ) { strcpy( str,pstr ); } char *gets() { return str; } friend string operator+( string obj1,string obj2 ); private: char str[100]; }; string operator+( string obj1,string obj2 ) { string tempobj; strcat( tempobj.str,obj1.str ); strcat( tempobj.str,obj2.str ); return tempobj; } void main() { string obj1( "Visual" ),obj2( " C++" ),obj3; obj3 = obj1 + obj2; cout << obj3.gets() << endl; } 2.定义一个整数计算类Integer,实现短整数 +,-,*,/ 基本算术运算。要求可以进行数据范围检查(-32768~32767,或自行设定),数据溢出时显示错误信息并中断程序运行。 答案 6.3-2 #include #include class Integer { private: short a; public: Integer (short n=0){ a=n;} Integer operator +(Integer); Integer operator -(Integer); Integer operator *(Integer); Integer operator /(Integer); Integer operator =(Integer); void display() {cout<}; Integer Integer::operator+(Integer x) { Integer temp; if(a+x.a<-32768||a+x.a>32767) {cout<<"Data overflow!"< return temp; } Integer Integer::operator-(Integer x) {Integer temp; if(a-x.a<-32768||a-x.a>32767) {cout<<"Data overflow!"< return temp; } Integer Integer::operator*(Integer x) {Integer temp; if(a*x.a<-32768||a*x.a>32767) {cout<<"Data overflow!"< return temp; } Integer Integer::operator/(Integer x) {Integer temp; if(a/x.a<-32768||a/x.a>32767) {cout<<"Data overflow!"< return temp; } Integer Integer::operator=(Integer x) { a=x.a; return *this; } void main() { Integer A(90),B(30),C; cout<<"A=";A.display(); cout<<"B=";B.display(); C=A+B; cout<<"C=A+B="; C.display(); C=A-B; cout<<"C=A-B="; C.display(); C=A*B; cout<<"C=A*B="; C.display(); C=A/B; cout<<"C=A/B="; C.display(); } 3.定义一个实数计算类Real,实现单精度浮点数 +,-,*,/ 基本算术运算。要求可以进行数据范围(-3.4×1038~3.4×1038,或自行设定)检查,数据溢出时显示错误信息并中断程序运行。 答案 6.3-3 #include #include class Real {private: double a; public: Real (double r=0){a=r;} Real operator +(Real); Real operator -(Real); Real operator *(Real); Real operator /(Real); Real operator =(Real); void display() {cout<}; Real Real::operator+(Real x) { Real temp; if(a+x.a<-1.7e308||a+x.a>1.7e308) {cout<<"Data overflow!"< return temp; } Real Real::operator-(Real x) { Real temp; if(a-x.a<-1.7e308||a-x.a>1.7e308) {cout<<"Data overflow!"< return temp; } Real Real::operator*(Real x) {Real temp; if(a*x.a<-1.7e308||a*x.a>1.7e308) {cout<<"Data overflow!"< return temp; } Real Real::operator/(Real x) {Real temp; if(a/x.a<-1.7e308||a/x.a>1.7e308) {cout<<"Data overflow!"< return temp; } Real Real::operator=(Real x) { a=x.a; return *this; } void main() { Real A(1.1),B(1.2),C; cout<<"A=";A.display(); cout<<"B=";B.display(); C=A+B; cout<<"C=A+B="; C.display(); C=A-B; cout<<"C=A-B="; C.display(); C=A*B; cout<<"C=A*B="; C.display(); C=A/B; cout<<"C=A/B="; C.display(); } 4.设向量X = ( x1 ,x2 ,x3) 和 Y = ( y1 , y2 ,y3 ),则它们之间的加、减和积分别定义为: X + Y = ( x1 + y1 , x2 + y2 , x3 + y3 ) X - Y = ( x1 - y1 , x2 - y2 , x3 - y3 ) X * Y = x1 * y1 + x2 * y2 + x3 * y3 编程序定义向量类vector, 重载运算符“+”、“-” 、“*”和“=”,实现向量之间的加、减、乘和赋值运算。用重载运算符“>>”、“<<”做向量的输入/输出操作。注意检测运算的合法性。 答案 6.3-4 #include #include class Vector {private: double *v; int len; public: Vector(int size); Vector(double *,int); ~Vector(); double &operator[](int i); Vector & operator =(Vector &); friend Vector operator +(Vector &,Vector &); friend Vector operator -(Vector &,Vector &); friend double operator *(Vector &,Vector &); friend ostream & operator <<(ostream &output,Vector &); friend istream & operator >>(istream &input,Vector &); }; Vector::Vector (int size) { if(size<=0||size>=21474837) {cout<<"The size of "< v=new double [size]; for(int i=0;i } Vector::Vector(double *C,int size) { if(size<=0||size>=21474837) {cout<<"The size of"< v=new double[size]; len=size; for(int i=0;i } Vector::~Vector() {delete []v; v=NULL; len=0; } double &Vector::operator[](int i) {if(i>=0 && i {cout<<"The size of"< abort();} } Vector &Vector::operator =(Vector &C) { if(len==C.len) {for(int i=0;i return *this; } else { cout<<"Operator = fail!\\n"; abort();} } Vector operator +(Vector &A,Vector &B) // 向量相加 { int size=A.len ; double *T=new double[size]; if(size==B.len) {for(int i=0;i return Vector (T,size); } else {cout<<"Operator + fail!\\n"; abort(); } } Vector operator -(Vector &A,Vector &B) //向量相减 { int size=A.len ; double *T=new double[size]; if(size==B.len) {for(int i=0;i return Vector (T,size); } else {cout<<"Operator - fail!\\n"; abort();} } double operator *(Vector &A,Vector &B) //向量相乘 { int size=A.len ; double s=0; if(size==B.len) {for(int i=0;i return s; } else {cout<<"Operator * fail!\\n"; abort();} } ostream & operator <<(ostream &output,Vector &A) //输出 { output<<'('; for(int i=0;i } istream & operator >>(istream &input,Vector &A) //输入 { for(int i=0;i return input; } void main() { int k1,k2,k3;double t; cout<<"Input the length of Vector A:\\n"; cin>>k1; Vector A(k1); cout<<"Input the elements of Vector A:\\n"; cin>>A; cout<<"Input the length of Vector B:\\n"; cin>>k2; Vector B(k2); cout<<"Input the elements of Vector B:\\n"; cin>>B; cout<<"Input the length of Vector C:\\n"; cin>>k3; Vector C(k3); cout<<"A="< cout<<"B="< C=A+B; cout<<"A+B="< C=A-B; cout<<"A-B="< t=A*B; cout<<"A*B="<} 5.定义一个类nauticalmile_kilometer,它包含两个数据成员kilometer(千米)和meter(米)。还包含一个构造函数对数据成员初始化;成员函数print用于输出数据成员kilometer和meter的值;类型转换函数double()实现把千米和米转换为海里(1海里=1.852千米)。编写main函数,测试类nauticalmile_kilometer。 答案 6.3-5 #include const double n = 1.852; // 定义海里与千米和米的转换系数(1海里=1.852千米) class nauticalmile_kilometer { public: nauticalmile_kilometer( int km,double m ) { kilometer = km; meter = m; } void print() { cout<<"kilometer="< private: int kilometer; double meter; }; nauticalmile_kilometer::operator double() { return ( meter / 1000 + double( kilometer )) / n; } void main() { nauticalmile_kilometer obj( 100,50 ); obj.print(); cout << "nauticalmile=" << double( obj ) << endl; }下载本文
可能会出现的问题: 运算符重载 定义 解释 调用 成员函数 Obj& operator op(); Obj.operator op() Obj op 或 op Obj 友员函数 friend Obj & operator op(Obj &); operator op(Obj) Obj o p 或 op Obj
6.3 编程题 定义形式 调用形式 调用时机 构造函数 ClassX::ClassX(arg,arg1=E1,...,argn=En); 隐式调用 建立对象、参数传递时 类类型转换函数 ClassX::operator Type(); 用类型符显式调用; 需要做数据类型转换时