zoukankan      html  css  js  c++  java
  • 动态生成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 }
  • 相关阅读:
    红黑树——以无厚入有间
    红黑树——依天理以神遇
    B-树 分合之道
    B-树 动机与结构
    云心出岫——Splay Tree
    双散列和再散列暨散列表总结
    开放定址法——平方探测(Quadratic Probing)
    [LeetCode 109]
    [LeetCode 110]
    [LeetCode 111]
  • 原文地址:https://www.cnblogs.com/Conan-jine/p/12590227.html
Copyright © 2011-2022 走看看