在类中可以使用数组,也可以将数组作为类的对象
a.直接在声明数组对象的时候初始化对象成员
#include<iostream>
using namespace std;
class area
{
public:
area(int l, int w){length=l;width=w;cout<<"调用析构函数设置长和宽的值"<<endl;}
~area(){cout<<"调用析构函数释放内存"<<endl;}
int get(){return length*width;}
// void set(int l, int w){length=l;width=w;}
private:
int length;
int width;
};
int main()
{
int i;
area one[4]={area(10,10),area(11,10),area(12,10),area(13,10),};
for (i=0;i<4;i++)
{
cout<<one[i].get()<<endl;
}
//cout<<one[0].get()<<endl;
return 0;
}
b.把对象名声明成一个数组
#include <iostream>
using namespace std;
class get
{
public:
int a[5];
};
int main()
{
get one[5];
int i;
for (i=0;i<5;i++)
{
cin>>one[i].a[i];
cout<<"one:"<<endl;
for (i=0;i<5;i++)
{
cout<<one[i].a[i];
}
return 0;
}