易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
C++对象数组的创建
使用一维指针创建对象数组:
#include <iostream>
#include <string>
using
namespace
std;
int
nextStudentID = 1;
class
StudentID
{
public
:
StudentID()
{
cout <<
"StudentID()"
<< endl;
value = nextStudentID++;
cout <<
"value:"
<< value << endl;
}
~StudentID()
{
--nextStudentID;
cout <<
"~StudentID()"
<< endl;
}
protected
:
int
value;
};
class
Student
{
public
:
Student(string pName =
"noName"
)
{
cout <<
"Student()"
<< endl;
name = pName;
cout <<
"name:"
<< name << endl;
}
~Student()
{
cout <<
"~Student()"
<< endl;
}
protected
:
string name;
StudentID id;
};
int
main(
int
argc,
char
**argv)
{
int
i;
cin >> i;
Student *p =
new
Student [i];
delete
[] p;
cout <<
"nextStudentID:"
<< nextStudentID << endl;
return
0;
}
结果:
[cpp]
>>3
StudentID()
value:1
Student()
name:noName
StudentID()
value:2
Student()
name:noName
StudentID()
value:3
Student()
name:noName
~Student()
~StudentID()
~Student()
~StudentID()
~Student()
~StudentID()
nextStudentID:1
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图