C++实验21

一、三维坐标类直接运算

#include<iostream>
using namespace std;

class point3d
{
    float x,y,z;
public:
    point3d(float a=0,float b=0,float c=0)
    {x=a;y=b;z=c;}
    point3d operator +(point3d const &a)
    {
        return point3d(a.x+x,a.y+y,a.z+z);
    }
    point3d operator =(point3d const &a)
    {   
        return point3d(a.x,a.y,a.z);
    }
    point3d operator --()
    {
        x--;y--;z--;
        return *this;
    }
    friend point3d operator --(point3d &,int);
    friend bool operator !=(point3d const &,point3d const &);
    void show()     {cout<<"("<<x<<","<<y<<","<<z<<")\n";}   
};

point3d operator --(point3d &a, int)
{
    point3d temp=a;//==已重载,故可直接使用赋值
    --a.x;
    --a.y;
    --a.z;
    return temp;
}
bool operator !=(point3d const &a,point3d const &b)
{
    return a.x==b.x && a.y==b.y && a.z==b.z?false:true;
}

void main()
{
    point3d a(3,4,5);
    point3d b(4,5,6);
    cout<<"a";
    a.show();
    cout<<"b";
    b.show();
    cout<<endl<<"c=a+b\tc";
    point3d c=a+b;
    c.show();
    cout<<"a!=b?\t"<<(a!=b)<<endl;
    b--;
    cout<<"b--\tb";
    b.show();
    cout<<"a!=b?\t"<<(a!=b)<<endl;
}

二、线性表类运算符成员函数重载

#include<iostream>
using namespace std;

class line
{
    float * list;
    int max;
    int num;
public:
    line(int b=0, int c=0)
    {
        max=b;
        num=c;
        if(num>max)
            cout<<"error, num must be smaller than max!"<<endl;
        list= new float[max];
        for(int i=0;i<num;i++)
        {
            cout<<"total "<<num<<" data, it is "<<i<<" now.\n";
            cin>>*(list+i);
        }
    }

    line(line &a)
    {
        max=a.max;
        num=a.num;
        list = new float[max];
        for(int i=0;i<num;i++)
            *(list+i)=*(a.list+i);
        cout<<"copy constructor"<<endl;
    }
    ~line() {delete []list;}

    void show()
    {
        for(int i=0;i<num;i++)
            cout<<*(list+i)<<'\t';
        cout<<endl;
    }
    line operator +(line &b)
    {
        line temp;
        temp.num=num+b.num;
        temp.max=max+b.max;
        temp.list=new float[num+b.num];
        for(int i=0;i<num;i++)
            *(temp.list+i)=*(list+i);
        for(int i=0;i<b.num;i++)
            *(temp.list+num+i)=*(b.list+i);
        return temp;


    }
    line  operator =(line &b)     
    {
        delete []list;
        list = new float [b.max];
        num=b.num;
        max=b.max;
        for(int i=0;i<num;i++)
            *(list+i)=*(b.list+i);
        return *this;            
    }

};

void main()
{
    cout<<"input a:\n";
    line a(5,2);
    cout<<"input b\n";
    line b(5,4);
    cout<<"a:\n";
    a.show();
    cout<<"b\n";
    b.show();

    line c;
    c=a+b;
    a=b;

    cout<<"a:\n";
    a.show();
    cout<<"b:\n";
    b.show();
    cout<<"c:\n";
    c.show();
}

三、线性表类运算符友元函数重载

#include<iostream>
using namespace std;

class line
{
    float * list;
    int max;
    int num;
public:
    line(int b=0, int c=0)
    {
        max=b;
        num=c;
        if(num>max)
            cout<<"error, num must be smaller than max!"<<endl;
        list= new float[max];
        for(int i=0;i<num;i++)
        {
            cout<<"total "<<num<<" data, it is "<<i<<" now.\n";
            cin>>*(list+i);
        }
    }

    line(line &a)
    {
        max=a.max;
        num=a.num;
        list = new float[max];
        for(int i=0;i<num;i++)
            *(list+i)=*(a.list+i);
        cout<<"copy constructor"<<endl;
    }
    ~line() {delete []list;}

    void show()
    {
        for(int i=0;i<num;i++)
            cout<<*(list+i)<<'\t';
        cout<<endl;
    }
    line  operator =(line &b)     
    {
        delete []list;
        list = new float [b.max];
        num=b.num;
        max=b.max;
        for(int i=0;i<num;i++)
            *(list+i)=*(b.list+i);
        return *this;            
    }
    friend line operator +(line &a,line &b);

};
    line operator +(line &a,line &b)
    {
        line temp;
        temp.num=a.num+b.num;
        temp.max=b.max+b.max;
        temp.list=new float[temp.max];
        for(int i=0;i<temp.num;i++)
            *(temp.list+i)=*(a.list+i);
        for(int i=0;i<b.num;i++)
            *(temp.list+a.num+i)=*(b.list+i);
        return temp;
    }

void main()
{
    cout<<"input a:\n";
    line a(5,2);
    cout<<"input b\n";
    line b(5,4);
    cout<<"a:\n";
    a.show();
    cout<<"b\n";
    b.show();

    line c;
    c=a+b;
    a=b;

    cout<<"a:\n";
    a.show();
    cout<<"b:\n";
    b.show();
    cout<<"c:\n";
    c.show();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值