以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://222.73.7.161/bbs/index.asp)
--  期货人生  (http://222.73.7.161/bbs/list.asp?boardid=7)
----  [原创]c++构造函数与析构函数  (http://222.73.7.161/bbs/dispbbs.asp?boardid=7&id=57276)

--  作者:z7c9
--  发布时间:2013/10/8 13:31:12
--  后台持仓函数分析说明(未完待续)

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

class Person{
public:
    Person(string name="Dennis",int age =34 ){
        cout << "   默认构造函数";
    }

    Person(char* name):name(string(name)),age(34){
        cout << "   转换构造函数1";
    }

    Person(double salary){
        cout << "   转换构造函数2";
    }

    Person(string name, int age,double salary):name(name),age(age){
        cout << "   一般构造函数1";
    }

    Person(string name,double salary):name(name){
        cout << "   一般构造函数2";
    }

    Person(const Person& p):name(p.name),age(p.age){
        cout << "   复制构造函数";
    }

    Person(Person&& p):name(p.name),age(p.age){
        p.name = "" ;
        p.age = 0;
        cout << "   移动构造函数";
    }

    ~Person(){
        cout << "   析构函数";
    }

private:
    string name;
    int age;
} ;

int main(){
    cout << endl << "p1 :";
    Person p1;
    cout << endl  << "p2 :";
    Person p2 = "Super";
    cout << endl << "p3 :";
    Person p3 = 50000;
    cout << endl << "p4 :";
    Person p4("Super",34,20000);
    cout << endl << "p5 :";
    Person p5("Dennis",30000.00);
    cout << endl << "p6 :";
    Person p6(p3);
    cout << endl << "p7 :";
    Person p7 = Person();
    cout << endl;
}