首页 > 编程语言 > 详细

C++构造函数2

时间:2016-10-25 01:51:39      阅读:240      评论:0      收藏:0      [点我收藏+]

一、构造函数分类

  普通构造函数,复制(拷贝)构造函数,赋值构造函数,

#include <iostream>
using namespace std;
class A {
public:
    A() { a = 0; }//普通
    A(const A&other) {//复制
        this->a = other.a;
    }
    A &operator=(const A & other) {//赋值
        this->a = other.a;
        return *this;
    }
A(double convert) {//转换构造函数
    this->a = int(convert);
}
private:
    int a;
};
int main()
{
    A a, b;//调用普通构造函数
    A c = b;//调用复制构造函数
    c = a;//调用赋值构造函数
    A d(c);//调用赋值构造函数
double e = 0.1;
A f(e);
    return 0;
}

C++构造函数2

原文:http://www.cnblogs.com/MyBlog-Richard/p/5994956.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!