对象数组的构造方法

来源:互联网 发布:淘宝精仿鞋店推荐 编辑:程序博客网 时间:2024/05/22 07:40
#include <iostream>
using namespace std;
class Person
{
    string name;
    int id;
public:
    Person(string name,int id)
    {
        this->name = name;
        this->id = id;
    }
    void display()
    {
        cout<<"name :"<<this->name<<","<<"id :"<<this->id<<endl;
    }
};
int main()
{
   Person *p[10];
   int n;
   cin>>n;
   for(int i=1;i<=n;i++)
   {
       p[i] = new Person("狗博", i);
   }
   for(int i=1;i<=n;i++)
   {
       p[i]->display();
   }
   return 0;
}

原创粉丝点击