#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();
}
onemoo
相关分类