64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include "stdlib.h"
using namespace std;
class CComplex
{
public:
CComplex(double r = 0, double i = 0)
{
real = r;
imag = i;
}
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;
}
CComplex& operator [](int n);
private:
CComplex objArray[4];
};
CComplex& CVector::operator[](int n)
{
if (n < 0 || n>3)
{
cout << "Out of range!" << endl;
exit(0);
}
return 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();
}