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


    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

  • 相关阅读:
    单张图像3D重建:原理与PyTorch实现
    34.Django视图
    备忘录模式:对象状态的保存与恢复
    ForkJoinPool在生产环境中使用遇到的一个问题
    【AI绘画接口】Midjourney是什么?Midjourney有官方接口吗?
    VUE的10个常用指令
    低度酒赛道进入洗牌期,新品牌如何破局三大难题?
    电子科技大学《数据库原理及应用》(持续更新)
    由于.git/config导致的Git存储库泄露
    ElasticSearch-Mapping详解
  • 原文地址:https://blog.csdn.net/w541541/article/details/134091807