首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++类中如何创建动态数组

2013-06-26 
c++类中怎么创建动态数组class student{//学生类}class cla{//班级类private: student s[100]}我想创建

c++类中怎么创建动态数组

class student{      //学生类
};
class cla{      //班级类

private:
 student s[100];
};

我想创建一个班级有很多个学生,想动态创建一个,根据输入学生人数的不同分配不同的大小,请问大神代码怎么写?
[解决办法]
动态分配使用new,析构函数里面使用delete;
#include <iostream>
using namespace std;
class student{      //学生类
public:
student(){
  cout<<"create function"<<endl;
}
~student(){
  cout<<"xigou function"<<endl;
}
};
class cla{      //班级类
 
private:
 student *sp;
 student s[5];
public:
 cla(int n);
 ~cla();
};
cla::cla(int n)
{
sp=new student[n];
}
cla::~cla()
{
//???
}
int main(){
   cla cla1(3);
}

热点排行
Bad Request.