运行时会显示:写入权限冲突,应该怎么改代码才能正常运行
题目:编写一个程序,用成员函数重载运算符+和-,将两个一维数组相加和相减,要求第一个一维数组的值由构造函数设置,另一个一维数组的值由键盘输入。
c++
#include<iostream>
using namespace std;
class Shape
{
private:
int *a;
public:
Shape();
Shape(int *b)
{
a = b;
}
void show();
Shape operator +(Shape &c);
Shape operator -(Shape &c);
};
Shape::Shape()
{
a = NULL;
}
void Shape::show()
{
for (int i = 0; i < 4; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
Shape Shape::operator + (Shape &c)
{
Shape temp;
for (int i = 0; i < 4; i++)
{
temp.a[i] = a[i] + c.a[i];
}
return temp;
}
Shape Shape::operator -(Shape &c)
{
Shape temp;
for (int i = 0; i < 4; i++)
{
temp.a[i] = a[i] - c.a[i];
}
return temp;
}
int main()
{
int a[4] = { 1,2,3,4 };
int b[4];
Shape obj(a);
obj.show();
cout<<"pleace input 4 numbers:"<<endl;
for(int j=0;j<4;j++)
{
cin>>b[j];
}
Shape obj1(b), obj2;
obj2 = obj1 + obj;
obj2.show();
system("pause");
return 0;
}