• 构造、清理、拷贝和移动简单实例


    Complex 复数类

    显示地写出构造、清理、拷贝和移动。

    Complex类

    class Complex
    {
    private:
    	double real = 3;      //复数的实部
    	double imag = 4;    //复数的虚部
    public:
    	Complex();	 			        /*无参构造*/
    	Complex(double a, double b);	/*有参构造*/
    	Complex(Complex& Z);            /*拷贝构造*/
    	Complex(Complex&& Z);           /*移动构造*/
    	~Complex();                     /*析构函数*/
    	Complex& operator = (Complex& Z);  /*拷贝赋值*/
    	Complex& operator = (Complex&& Z); /*移动赋值*/
    	void Complex_Printf(void);
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Complex类实现

    /*无参构造*/
    Complex::Complex()
    {
    	cout << "无参构造:";
    }
    /*有参构造*/
    Complex::Complex(double a, double b) :real(a), imag(b)
    {
    	cout << "有参构造:";
    }
    /*拷贝构造*/
    Complex::Complex(Complex& Z) :real(Z.real), imag(Z.imag)
    {
    	cout << "复制构造:";
    }
    /*移动构造*/
    Complex::Complex(Complex&& Z) :real(Z.real), imag(Z.imag)
    {
    	cout << "移动构造:";
    }
    /*析构函数*/
    Complex::~Complex()
    {
    	cout << "析构函数" << endl;
    }
    /*拷贝赋值*/
    Complex& Complex::operator = (Complex& Z)
    {
    	real = Z.real;
    	imag = Z.imag;
    	cout << "拷贝赋值:";
    	return *this;
    }
    /*移动赋值*/
    Complex& Complex::operator = (Complex&& Z)
    {
    	real = Z.real;
    	imag = Z.imag;
    	cout << "移动赋值:";
    	return *this;
    }
    void Complex::Complex_Printf(void)
    {
    	cout << real << " + " << imag << "i" << endl;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    测试:

    #include "Complex.h"
    
    #include 
    using namespace std;
    
    Complex GetComplex()
    {
    	//	Complex Z1{ 2, 5 };
    	Complex Z2{ 4, 8 };
    	return Z2;
    }
    
    
    int main(void)
    {
    	Complex Z1;/*无参构造*/
    	Z1.Complex_Printf();
    	Complex Z2 = { 2, 5 };/*有参构造*/
    	Z2.Complex_Printf();
    	Complex Z3 = { Z1 };/*拷贝构造*/
    	Z3.Complex_Printf();
    	Z3 = Z2;  /*拷贝赋值*/
    	Z3.Complex_Printf();
    	Complex Z4 = move(Z1);   /*移动构造*/
    	Z4.Complex_Printf();
    	Z4 = GetComplex(); /*移动赋值*/
    	Z4.Complex_Printf();
    	/*std:move() 强制变成右值*/
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    结果:

    Complex1

  • 相关阅读:
    Push和Pull两种类型的消费者
    Java工具——Eclipse设置字体大小
    7_JS关于数据代理_Object.defineProperty_Vue数据代理_双向绑定
    ZooKeeper(一):基础介绍
    选择智慧公厕解决方案,开创智慧城市公共厕所新时代
    跨平台应用开发进阶(二十三) :一文走近 testflight 上架
    90%的人以为会用ThreadPoolExecutor了,看了这10张图再说吧
    mysql存储过程与函数
    电脑屏幕模糊?这5个方法教你恢复清晰屏幕!
    Vue3系列1--配置环境和创建项目
  • 原文地址:https://blog.csdn.net/w541541/article/details/134091807