C++构造函数2

时间:2023-03-09 12:49:12
C++构造函数2

一、构造函数分类

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

#include <iostream>
using namespace std;
class A {
public:
A() { a = ; }//普通
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 ;
}