欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

重载赋值运算符

程序员文章站 2022-06-06 22:19:41
``` // // main.cpp // 运算符重载(Overloading) // 默认复制构造函数(Default Copy Constructor) // Created by mac on 2019/4/29. // Copyright © 2019年 mac. All rights re ......
//
//  main.cpp
//  运算符重载(overloading)
//  默认复制构造函数(default copy constructor)
//  created by mac on 2019/4/29.
//  copyright © 2019年 mac. all rights reserved.
//  析构函数是一个成员函数,当对象被销毁时,该函数被自动调用。
//  析构函数是具有与类相同名称的公共(public)成员函数
//  构造函数私有化之后就无法在类外生成对象。
//  结构体能否支持继承
//  复制构造函数只能在创建对象并在初始化对象时起作用,复制构造函数不能在赋值(assignment)中调用。
//  复制构造函数开始有点遗忘了。


#include <iostream>
#include <iomanip>

using namespace std;

class numberarray{
private:
    double *aptr;
    int arraysize;

public:
    numberarray & operator= (const numberarray &right);//重载赋值运算符=      (重载运算符函数)
    ~numberarray(){if (arraysize>0) delete []aptr;}
    numberarray(const numberarray &);
    numberarray(int size,double value);
    void print()const;
    void setvalue(double value);
};

//重载赋值运算符=
numberarray & numberarray::operator=(const numberarray &right){
    if(this!=&right){
        if(this->arraysize>0){
            delete [] aptr;
        }
        
        this->arraysize=right.arraysize;
        aptr=new double[this->arraysize];
        for (int index=0; index<arraysize; ++index) {
            this->aptr[index]=right.aptr[index];
        }
        
    }
    return *this;
}

//复制构造函数
numberarray::numberarray(const numberarray &obj){
    if(this->arraysize>0){
        delete [] aptr;
    }
    this->arraysize=obj.arraysize;
    aptr=new double[this->arraysize];
    for (int index=0; index<this->arraysize; ++index) {
        aptr[index]=obj.aptr[index];
    }
    
}

//构造函数
numberarray::numberarray(int size,double value){
    this->arraysize=size;
    aptr=new double[arraysize];
    setvalue(value);
}

//用value值初始化整个数组
void numberarray::setvalue(double value){
    for (int index=0; index<this->arraysize; ++index) {
        aptr[index]=value;
    }
    
}

//打印整个数组
void numberarray::print()const{
    for (int index=0;index<arraysize; ++index) {
        cout<<aptr[index]<<" ";
    }
}

int main(int argc, const char * argv[]) {
    numberarray first(3,10.5);
    numberarray second(5,20.5);
    cout<<setprecision(2)<<fixed<<showpoint;
    cout<<"first object's data is ";
    first.print();
    cout<<endl<<"second object's data is ";
    second.print();
    
    //调用重载运算符
    cout<<"\nnow we will assign the second object "<<"to the first."<<endl;
    
    first=second;
    
    //打印赋值以后两个对象里面的新的值
    cout<<"first object's data is ";
    first.print();
    cout<<endl<<"second object's data is ";
    second.print();
    
    return 0;
}

运行结果:

first object's data is 10.50 10.50 10.50 
second object's data is 20.50 20.50 20.50 20.50 20.50 
now we will assign the second object to the first.
first object's data is 20.50 20.50 20.50 20.50 20.50 
second object's data is 20.50 20.50 20.50 20.50 20.50 program ended with exit code: 0

tips

  • c++的输出格式设置问题?