1·题目介绍
2·参考程序
#include<iostream>
using namespace std;
class Repeat
{
private:
int a[3];
public:
Repeat(){}
Repeat(int *p)
{
int i;
for (i = 0; i < 3; i++)
{
a[i] = p[i];
}
}
friend Repeat operator^(Repeat& t,Repeat &r)
{
int i,j;
for (i = 0; i < 3; i++)
{
if (r.a[i] == 0)
{
t.a[i] = 1;
}
else
{
int m = t.a[i];
for (j = 0; j < r.a[i]-1; j++)
{
t.a[i] *= m;
}
}
}
return t;
}
void print()
{
int i;
for (i = 0; i < 3; i++)
{
cout << a[i] << '\t';
}
cout << endl;
}
};
int main()
{
int a[3] = { 2,2,2 };
int b[3] = { 3,3,3 };
Repeat t1(a);
Repeat t2(b);
Repeat t3;
t3 = t1 ^ t2;
t3.print();
return 0;
}
3·心得收获
a·当我们要在构造函数中初始化数组时,要将构造函数中的形参尽可能定义为指针变量
不要再定义为数组了