欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-22 09:28:14
1 #include 2 #include 3 4 using namespace std; 5 6 class Person 7 { 8 private: 9 string name; 10 int age; 11 char sex; 12 public: ......
 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 }