c++创建动态数组,怎么在主函数里用键盘输入数组元素个数?

利用下面已定义的Employee类,在主程序中声明包含若干个元素的对象数组,每个元素都是Employee类型的对象。要求从键盘输入数组大小,建立动态数组。
class Employee{
private:
string name;
string address;
string city;
string postcode;
public:
Employee():name("a"),address("b"),city("c"),postcode("d"){}
Employee(string n,string a,string c,string p):name(n),address(a),city(c),postcode(p){
cout<<"End Entering"<<endl;}
~Employee(){cout<<"End"<<endl;}
void setName(){
string n;
cout<<"Enter Nmae:";
cin>>n;
name=n;}
void display(){
cout<<"Name:"<<name<<endl;
cout<<"Address:"<<address<<endl;
cout<<"City:"<<city<<endl;
cout<<"Postcode:"<<postcode<<endl;
cout<<"Displaying all"<<endl;}
};

第1个回答  2013-06-05
int main()
{
Employee *E;
int amount;
cin >> amount;
E = new Employee [amount];
...
delete [] E; //最后记得释放空间
return 0;
}本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网