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 }
  • 相关阅读:
    Date日期对象
    JAVA适配器
    java 对象的多态性
    简单轮播
    ecshop 教程地址
    瀑布流js排列
    phpcms 搜索结果页面栏目不显示解决 方法
    手机自动跳转
    字串符转换数字、取小数点后两位数的方法
    js 判断鼠标进去方向
  • 原文地址:https://www.cnblogs.com/Conan-jine/p/12590227.html
Copyright © 2011-2022 走看看