#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>