自考新教材--p107

源程序:

#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 << "内存分配错误\n";

    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;

}

 运行结果:

猜你喜欢

转载自www.cnblogs.com/duanqibo/p/12081972.html