57 lines
940 B
C++
57 lines
940 B
C++
|
// 构造函数的分类与调用
|
||
|
#include <iostream>
|
||
|
#include <bitset>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
class C
|
||
|
{
|
||
|
private:
|
||
|
int x, y;
|
||
|
|
||
|
public:
|
||
|
C(int x)
|
||
|
{
|
||
|
this->x = x;
|
||
|
}
|
||
|
C(int x, int y)
|
||
|
{
|
||
|
this->x = x;
|
||
|
this->y = y;
|
||
|
}
|
||
|
C(const C &other)
|
||
|
{
|
||
|
this->x = other.x;
|
||
|
this->y = other.y;
|
||
|
}
|
||
|
void show()
|
||
|
{
|
||
|
cout << "x = " << x << ", y = " << y << endl;
|
||
|
}
|
||
|
~C()
|
||
|
{
|
||
|
cout << this << "释放资源" << endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
cout << "C(0,1) 之前" << endl;
|
||
|
C(0, 1); // 匿名创建的类对象,创建完后就直接释放了
|
||
|
cout << "C(0,1) 之后" << endl;
|
||
|
|
||
|
C c1(1, 2);
|
||
|
c1.show();
|
||
|
C c2 = (1, 5); // 使用单参数构造函数 C(int x), 然后将其改为 ", 运算" 后面的值
|
||
|
// C c2 = 1;
|
||
|
c2.show();
|
||
|
C c3 = C(3, 4);
|
||
|
c3.show();
|
||
|
C c4(c3);
|
||
|
c4.show();
|
||
|
C &c5 = c4;
|
||
|
c5.show();
|
||
|
|
||
|
return 0;
|
||
|
}
|