关于重载[]的问题,帮帮我
这个人源程序,帮我看一下哪错了谢谢
#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;
}
CVector &operator [](int n);
private:
CComplex objArray[4];
};
CComplex &CVector::&operator[](int n);
};
void CVector::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();
}
...全文
相关推荐
重载函数实现变量排序问题
本文将详细介绍如何通过函数重载来解决变量排序的问题,并提供具体的代码示例。#### 一、问题描述本任务要求实现一个程序,该程序能够对两个整数和三个浮点数进行排序,并按照从小到大的顺序输出排序结果。
详解C++ 重载运算符和重载函数
C++ 重载运算符和重载函数是编程中的一种高级特性,它允许开发者在同一个作用域内为相同名称的操作提供多种实现方式,从而增加代码的灵活性和表达力。这两种重载主要涉及到函数和运算符的多态性应用。1.
C++ 重载运算符和重载函数
"C++ 重载运算符和重载函数"C++ 语言的一个关键特性是函数重载和运算符重载,这使得程序员能够根据需要定制和扩展语言的功能。函数重载允许在同一个作用域内声明多个同名函数,但每个函数的参数列表必
java方法重载示例
这就是方法重载的正确应用,编译器可以通过参数列表来区分它们。### 4. 方法重载的注意事项- **返回类型不参与重载判断**:如前所述,方法的返回类型不能作为区分重载方法的依据。
C++ new、delete(new[]、delete[])操作符重载需要注意的问题
当需要为类或对象分配内存时,程序员可以重载这些操作符以自定义内存管理的行为。然而,重载`new`、`delete`操作符需要注意一些关键问题,以确保正确性和兼容性。1.


