一段老的代码,关于重载的
//point.h
#ifndef POINT_CPP
#define POINT_CPP
class point{
protected:
double x,y;
public:point(double xx=0,double yy=0){
x=xx;y=yy;
}
point operator=(point);
double magnitude();
double angle();
point operator+(point);
/* #ifndef _ZTC_
point operator+();
#endif */
point operator-(point);
// point operator-();
point operator*(double);
point operator/(double);
point operator%(point);
point operator+=(point);
point operator-=(point);
point operator*=(double);
point operator/=(double);
point operator%=(point);
point operator++();
point operator--();
point operator[](double);
point operator<<(double);
point operator>>(double);
point operator<<=(double);
point operator>>=(double);
bool operator<(point);
bool operator>(point);
bool operator<=(point);
bool operator>=(point);
bool operator==(point);
bool operator!=(point);
bool operator!();
bool operator&&(point);
bool operator||(point);
double operator&(point);
double operator|(point);
double operator^(point);
point operator&=(point);
point operator|=(point);
point operator^=(point);
void print(char* msg="");
};
#endif
//point.cpp
#include "point.h"
#include <math.h>
#include <stdio.h>
const double tiny=0.0001;
point point::operator=(point rv){
x=rv.x;
y=rv.y;
return *this;
}
double point::magnitude(){
return sqrt(x*x+y*y);
}
double point::angle(){
return atan2(y,x);
}
point point::operator+(point p){
return point(x+p.x,y+p.y);
}
/*#ifndef _ZTC_
point point::operator+(){
return *this;
}
#endif*/
point point::operator-(point p){
return point(x-p.x,y-p.y);
}
/*point point::operator-(){
return point(-x,-y);
}*/
point point::operator*(double f){
return point(x*f,y*f);
}
point point::operator/(double f){
return point(x/f,y/f);
}
point point::operator%(point p){
return point(fmod(x,p.x),fmod(y,p.y));
}
point point::operator+=(point p){
x+=p.x;
y+=p.y;
return *this;
}
point point::operator-=(point p){
x-=p.x;
y-=p.y;
return *this;
}
point point::operator*=(double f){
x*=f;
y*=f;
return *this;
}
point point::operator/=(double f){
x/=f;
y/=f;
return *this;
}
point point::operator%=(point p){
x=floor(x/p.x);
y=floor(y/p.y);
return *this;
}
point point::operator++(){
x+=1.0;
y+=1.0;
return *this;
}
point point::operator--(){
x-=1.0;
y-=1.0;
return *this;
}
point point::operator[](double f){
double new_x=magnitude()*cos(angle()*f);
double new_y=magnitude()*sin(angle()*f);
return point(new_x,new_y);
}
point point::operator<<(double f){
return point(x+f,y);
}
point point::operator>>(double f){
return point(x,y+f);
}
point point::operator<<=(double f){
x+=f;
return *this;
}
point point::operator>>=(double f){
y+=f;
return *this;
}
bool point::operator<(point p){
if(x<p.x&&y<p.y) return true;
return false;
}
bool point::operator>(point p){
if(x>p.x&&y>p.y) return true;
return false;
}
bool point::operator<=(point p){
if(x<=p.x&&y<=p.y) return true;
return false;
}
bool point::operator>=(point p){
if(x>=p.x&&y>=p.y) return true;
return false;
}
bool point::operator==(point p){
if(x==p.x&&y==p.y) return true;
return false;
}
bool point::operator!=(point p){
if(x!=p.x&&y!=p.y) return true;
return false;
}
bool point::operator!(){
if(fabs(x)<tiny&&fabs(y)<tiny) return true;
return false;
}
bool point::operator&&(point p){
if(fabs(x)<tiny&&fabs(y)<tiny) return false;
if(fabs(p.x)<tiny &&fabs(p.y)<tiny) return false;
return true;
}
bool point::operator||(point p){
if(fabs(x)<tiny&&fabs(y)<tiny&&fabs(p.x)<tiny&&fabs(p.y)<tiny) return false;
return true;
}
double point:: operator&(point p){
return magnitude()*p.magnitude()*sin(point::operator^(p));
}
double point::operator|(point p){
return magnitude()*p.magnitude()*cos(point::operator^(p));
}
double point::operator^(point p){
return fabs(angle()-p.angle());
}
point point::operator&=(point p){
double cross = magnitude()*p.magnitude()*sin(point::operator^(p));
x*=cross;
y*=cross;
return *this;
}
point point::operator|=(point p){
double dot=magnitude()*p.magnitude()*cos(point::operator^(p));
x*=dot;
y*=dot;
return *this;
}
point point::operator^=(point p){
double arc=fabs(angle()-p.angle());
x*=arc;
y*=arc;
return *this;
}
void point::print(char* msg){
if(*msg) printf("%s:",msg);
printf("x=%f,y=%f\n",x,y);
}
int main(){
point A,B(1.1,2.2),C(3.3,4.4),D(5.5,6.6);
B.print("B");
C.print("C");
D.print("D");
A=B+C-D;
A.print("A=B+C-D");
A+=(B<<8.2)+(D>>4.1);
A.print("A+=(B<<8.2)+(D>>4.1)");
printf("magnitude of A=%f, angle of A=%f\n",A.magnitude(),A.angle());
printf("Angle C^D=%f\n",C^D);
return 1;
}
[此贴子已经被作者于2005-9-22 15:28:39编辑过]