h-----------------------------
#include "newandmalloc.h"
#include <iostream>
using namespace std; newAndMalloc::newAndMalloc()
{
cout<<"construct newAndMalloc..."<<endl;
} newAndMalloc::~newAndMalloc()
{
cout<<"destruct newAndMalloc..."<<endl;
deletePInt();
deletePPInt();
deletePPPInt();
} void newAndMalloc::newPInt()
{
cout<<"new m_pInt..."<<endl; m_pInt = new int[m_size]; //开辟空间 for (int i = ; i < m_size; i++) //赋值
{
m_pInt[i] = m_size - i;
}
} void newAndMalloc::newPPInt()
{
cout<<"new m_ppInt..."<<endl; m_ppInt = new int*[m_size]; //开辟一维的空间
for (int i = ; i < m_size; i++)
{
m_ppInt[i] = new int[m_size]; //开辟二维的控件
} for (int i = ; i < m_size; i++) //赋值
{
for (int j = ; j < m_size; j++)
{
m_ppInt[i][j] = i + j;
}
} } void newAndMalloc::newPPPInt()
{
cout<<"new m_pppInt..."<<endl; m_pppInt = new int**[m_size]; //开辟一维的空间
for(int i = ; i < m_size; i++)
{
m_pppInt[i] = new int*[m_size]; //开辟二维的空间
for (int j = ; j < m_size; j++)
{
m_pppInt[i][j] = new int[m_size]; //开辟三维的空间
}
} for (int i = ; i < m_size; i++) //赋值
{
for (int j = ; j < m_size; j++)
{
for (int h = ; h < m_size; h++)
{
m_pppInt[i][j][h] = i + j + h;
}
}
}
} void newAndMalloc::printPInt()
{
cout<<"print m_pInt..."<<endl; for (int i = ; i < m_size; i++)
{
cout<<m_pInt[i]<<" ";
}
cout<<endl;
} void newAndMalloc::printPPInt()
{
cout<<"print m_ppInt..."<<endl; for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
cout<<m_ppInt[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
} void newAndMalloc::printPPPInt()
{
cout<<"print m_pppInt..."<<endl; for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
for (int h = ; h < m_size; h++)
{
cout<<m_pppInt[i][j][h]<<" ";
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
} void newAndMalloc::deletePInt()
{
cout<<"delete m_pInt..."<<endl;
delete[] m_pInt;
} void newAndMalloc::deletePPInt()
{
cout<<"delete m_ppInt..."<<endl;
for (int i = ; i < m_size; i++)
{
delete[] m_ppInt[i];
}
delete[] m_ppInt;
} void newAndMalloc::deletePPPInt()
{
cout<<"delete m_pppInt..."<<endl;
for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
delete[] m_pppInt[i][j];
}
delete[] m_pppInt[i];
}
delete[] m_pppInt;
} testClass::testClass()
{
cout<<"construct testClass..."<<endl;
} testClass::~testClass()
{
cout<<"destruct testClass..."<<endl;
} cpp--------------------------------
#include "newandmalloc.h"
#include <iostream>
using namespace std; newAndMalloc::newAndMalloc()
{
cout<<"construct newAndMalloc..."<<endl;
} newAndMalloc::~newAndMalloc()
{
cout<<"destruct newAndMalloc..."<<endl;
deletePInt();
deletePPInt();
deletePPPInt();
} void newAndMalloc::newPInt()
{
cout<<"new m_pInt..."<<endl; m_pInt = new int[m_size]; //开辟空间 for (int i = ; i < m_size; i++) //赋值
{
m_pInt[i] = m_size - i;
}
} void newAndMalloc::newPPInt()
{
cout<<"new m_ppInt..."<<endl; m_ppInt = new int*[m_size]; //开辟一维的空间
for (int i = ; i < m_size; i++)
{
m_ppInt[i] = new int[m_size]; //开辟二维的控件
} for (int i = ; i < m_size; i++) //赋值
{
for (int j = ; j < m_size; j++)
{
m_ppInt[i][j] = i + j;
}
} } void newAndMalloc::newPPPInt()
{
cout<<"new m_pppInt..."<<endl; m_pppInt = new int**[m_size]; //开辟一维的空间
for(int i = ; i < m_size; i++)
{
m_pppInt[i] = new int*[m_size]; //开辟二维的空间
for (int j = ; j < m_size; j++)
{
m_pppInt[i][j] = new int[m_size]; //开辟三维的空间
}
} for (int i = ; i < m_size; i++) //赋值
{
for (int j = ; j < m_size; j++)
{
for (int h = ; h < m_size; h++)
{
m_pppInt[i][j][h] = i + j + h;
}
}
}
} void newAndMalloc::printPInt()
{
cout<<"print m_pInt..."<<endl; for (int i = ; i < m_size; i++)
{
cout<<m_pInt[i]<<" ";
}
cout<<endl;
} void newAndMalloc::printPPInt()
{
cout<<"print m_ppInt..."<<endl; for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
cout<<m_ppInt[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
} void newAndMalloc::printPPPInt()
{
cout<<"print m_pppInt..."<<endl; for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
for (int h = ; h < m_size; h++)
{
cout<<m_pppInt[i][j][h]<<" ";
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
} void newAndMalloc::deletePInt()
{
cout<<"delete m_pInt..."<<endl;
delete[] m_pInt;
} void newAndMalloc::deletePPInt()
{
cout<<"delete m_ppInt..."<<endl;
for (int i = ; i < m_size; i++)
{
delete[] m_ppInt[i];
}
delete[] m_ppInt;
} void newAndMalloc::deletePPPInt()
{
cout<<"delete m_pppInt..."<<endl;
for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
delete[] m_pppInt[i][j];
}
delete[] m_pppInt[i];
}
delete[] m_pppInt;
} testClass::testClass()
{
cout<<"construct testClass..."<<endl;
} testClass::~testClass()
{
cout<<"destruct testClass..."<<endl;
} main-------------------------------
#include <iostream>
#include <stdio.h>
#include "newandmalloc.h" int main(int argc, char *argv[])
{
newAndMalloc *nam = nullptr;
nam = new newAndMalloc();
nam->newPInt();
nam->newPPInt();
nam->newPPPInt(); nam->printPInt();
nam->printPPInt();
nam->printPPPInt(); delete nam;
return ;
} 打印输出------------------------
construct newAndMalloc...
new m_pInt...
new m_ppInt...
new m_pppInt...
print m_pInt... print m_ppInt... print m_pppInt... destruct newAndMalloc...
delete m_pInt...
delete m_ppInt...
delete m_pppInt...
05-17 21:56