动态生成Person类的对象 代码参考

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Person
 7 {
 8     private:
 9         string name;
10         int age;
11         char sex;
12     public:
13         Person(){name="XXX";age=0;sex='m';}
14         void Register(string,int,char);
15         void Showme();
16         ~Person();
17 };
18 
19 void Person::Register(string name, int age, char sex)
20 {
21     this->name=name;
22     this->age=age;
23     this->sex=sex;
24     return;
25 }
26 
27 void Person::Showme()
28 {
29     cout<<name<<' '<<age<<' '<<sex<<endl;
30     return;
31 }
32 
33 Person::~Person()
34 {
35     cout<<"Now destroying the instance of Person"<<endl;
36 }
37 
38 int main()
39 {
40     string name;
41     int age;
42     char sex;
43     cin>>name>>age>>sex;
44     Person *p1,*p2;
45     p1=new Person();
46     p2=new Person();
47     cout<<"person1:";
48     p1->Showme();
49     cout<<"person2:";
50     p2->Showme();
51     p1->Register(name,age,sex);
52     p2->Register(name,age,sex);
53     cout<<"person1:";
54     p1->Showme();
55     cout<<"person2:";
56     p2->Showme();
57     delete p1;
58     delete p2;
59     return 0;
60 }

 

posted @ 2020-03-28 23:34  Conan-jine  阅读(498)  评论(0编辑  收藏  举报