一些常见的运算符重载

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>

using namespace std;

class   my_complex
{

public:

void * operator new(size_t size)
{
     void *p = (void *)malloc(size);
	 if(NULL != p)
	 {
		 cout << "operator new rom is sucess!"<< endl;
		 return p;
	 }

	
}
void operator delete (void *p)
{
	free(p);
}

public :
	void   get_data(void )
	{
		cout << "real = "<< real<<endl;
		cout << "virt = "<< virt << endl;

	}

private:
     double real;
	 double virt;

public:
	my_complex(){}
    my_complex(double x,double y)
	{
        real = x;
		virt = y;
	}

	my_complex (my_complex & sd)
	{
		real = sd.real;
		virt = sd.virt;
	}
	my_complex operator = (my_complex &sd)
	{
		if(this != &sd)
		{
			real = sd.real;
			virt = sd.virt;
		}
		return *this;
	}
    ~my_complex()
	{}
};

int main()
{
    my_complex a;
	char str[10];
	my_complex::operator new (sizeof(str));
	return 0;
}
运行结果:
operator new rom is sucess!
Press any key to continue
<div style="top: 654px;">
</div><div style="top: 654px;">输入输出运算符的重载</div><div style="top: 654px;">
</div><div style="top: 654px;">
</div><div style="top: 654px;">
#include<iostream>  
#include<stdio.h>
#include<stdlib.h></div><div style="top: 654px;">using namespace std;  
  
class value  
{  
public:  
                     //构造函数  
    value()  
    {  
        value1=0;  
        value2=0;  
    }  
    value(int v1,int v2)  
    {  
        value1=v1;  
        value2=v2;  
    }  
      
        //重载output  
    friend ostream &operator<< (ostream &output,const value &sw)  
    {  
        output<<"v1="<<sw.value1<<" "<<"v2="<<sw.value2;  
        return output;  
    }  
        //重载input  
    friend istream &operator>> (istream &input,value &sw)  
    {  
        input>>sw.value1>>sw.value2;  
        return input;  
    }  
          
    private:  
        int value1;  
        int value2;  
};  
  
int main()  
{  
    value sw1(10,20),sw2(5,8),sw3;  
  
    cout<<"Enter sw3:"<<endl;  
    cin>>sw3;  
  
    cout<<"sw1:"<<sw1<<endl;  
    cout<<"sw2:"<<sw2<<endl;  
    cout<<"sw3:"<<sw3<<endl;  
      
    return 0;  
}  </div><div style="top: 654px;">
</div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值