源程序:
#include <iostream>
using namespace std;
class samp
{
public:
void setij(int a, int b)
{
i = a;
j = b;
}
~samp()
{
cout << "析构..." << endl;
}
int getmuti()
{
return i*j;
}
protected:
int i, j;
};
int main()
{
samp *p;
p = new samp[5];
if (!p)
{
cout << "内存分配错误 ";
return 1;
}
for (int j = 0; j<5; j++)
p[j].setij(j, j);
for (int k = 0; k<5; k++)
cout << "muti[" << k << "]值是:" << p[k].getmuti() << endl;
delete[]p;
system("pause");
return 1;
}
运行结果: