对象的动态建立和释放
#include<iostream> using namespace std; class Box { public: Box(int w,int l,int h); ~Box(); int width; int length; int height; }; Box::Box(int w,int l,int h) { width=w; length=l; height=h; cout<<"========调用构造函数=======\n"; } Box::~Box() { cout<<"========调用析构函数=======\n"; } int main() { Box * p=new Box(12,13,15); cout<<p->width<<"\t"; cout<<p->length<<"\t"; cout<<p->height<<endl; delete p; return 0; }
建立指针数组
#include <iostream> using namespace std; class Time { public: Time(int,int,int); void get_time( ); private: int hour,minute,sec; }; Time::Time(int h,int m,int s):hour(h),minute(m),sec(s) {} int main() { const int N = 100; Time *t[N]= {NULL}; int i; i=3; if(t[i]==NULL) t[i] = new Time(10, 10,10); if(t[i]!=NULL) delete t[i]; return 0; }
时间: 2024-10-25 05:05:03