#include <iostream.h>#include "stdlib.h"class CComplex{public:CComplex(double r = 0, double i = 0){ real = r; imag = i;}int operator int(){ return (int)real;}void Display(void){ cout << "(" << real << "," << imag << ")" << endl;}protected:double real;double imag;};class CVector{public:CVector(CComplex &obj1, CComplex &obj2, CComplex &obj3, CComplex &obj4){ objArray[0] = obj1; objArray[1] = obj2; objArray[2] = obj3; objArray[3] = obj4;}friend CComplex &operator[](CVector obj, int n);private:CComplex objArray[4];};CComplex &operator[](CVector obj, int n){if(n<0 || n>3){ cout<<"Out of range!"<<endl; exit(0);}return obj.objArray[n];}void main(){CComplex c1(1.1, 1.1);CComplex c2(2.2, 2.2);CComplex c3(3.3, 3.3);CComplex c4(4.4, 4.4);CVector v(c1,c2,c3,c4);v[0].Display();v[1].Display();v[2].Display();v[3].Display();v[0] = 5.5; ----------------------------------------------------------①v[1] = CComplex(6.6); -------------------------------------------②v[2] = int(CComplex(7.7)); --------------------------------------③v[3] = int(CComplex(8.8,9.9)); ----------------------------------④v[0].Display();v[1].Display();v[2].Display();v[3].Display();}
1 回答
已采纳
onemoo
TA贡献883条经验 获得超454个赞
CComplex 中的转型函数 operator int() 前面不用写返回类型。
重载下标操作符 operator[] 必须是成员函数。 当然其参数只需要一个int就可以了。
main函数的返回类型是int,不要写成void。
- 1 回答
- 0 关注
- 2187 浏览
添加回答
举报
0/150
提交
取消