2012年(1008)
分类:
2012-08-01 11:08:36
原文地址:运算符重载 作者:luozhiyong131
/*
* 运算符重载
* 运算符重载就是给已有运算符赋予更多的含义,
* 使它能够用于特定类的对象,执行特定的功能,
* 而且使用形式与基本类型数据类型的形式相同
* 运算符重载实际上就是函数重载
*/
/*
* 使用普通函数完成自定义的加法运算
* Lzy 2011-8-8
*/
#include
using namespace std;
class ClassA
{
private:
int x, y;
public:
ClassA(){}
ClassA(int _x, int _y):x(_x),y(_y){}
ClassA add(ClassA &a, ClassA &b)
{
this->x = a.x + b.x;
this->y = a.y + b.y;
return *this;
}
void display(){cout<
};
int main(void)
{
ClassA a(1,1),b(2,2);
ClassA c;
c.add(a,b);
c.display();
}
/*
* 运算符重载
* 运算符通常是针对类中的私有成员进行操作,因此重载运算符应该能够访问类中的私有成员
* 所以运算符重载一般采用成员函数或友元函数的方式
*/
/*
* 成员函数重载运算符"+"
* Lzy 2011-8-8
*/
#include
using namespace std;
class ClassA
{
private:
int x, y;
public:
ClassA(){}
ClassA(int _x, int _y):x(_x),y(_y){}
ClassA operator + (ClassA &a)
{
ClassA c;
c.x = this->x + a.x;
c.y = this->y + a.y;
return c;
}
void display(){cout<
};
/*
* obj3 = obj1 + obj2;
* 运算符"+"可以访问两个对象:运算符左侧对象obj1是将要调用重载运算符函数的对象,
* 右侧对象obj2作为函数的参数
*/
int main(void)
{
ClassA a(1,1),b(2,2);
ClassA c;
c = a + b;
c.display();
}
/*
* 友元函数重载运算符"*"
* Lzy 2011-8-8
*/
#include
using namespace std;
class ClassA
{
private:
int x, y;
public:
ClassA(){}
ClassA(int _x, int _y):x(_x),y(_y){}
void display(){cout<
friend ClassA operator * (ClassA &a1, ClassA &a2);
};
ClassA operator * (ClassA &a1, ClassA &a2)
{
ClassA b;
b.x = a1.x * a2.x;
b.y = a1.y * a2.y;
return b;
}
int main(void)
{
ClassA a(2,2),b(2,2);
ClassA c;
c = a * b;
c.display();
}
/*
* 成员函数重载运算符前缀"++"
* Lzy 2011-8-8
*/
#include
using namespace std;
class CPoint
{
private:
int x, y;
public:
CPoint(int _x=0, int _y=0):x(_x),y(_y){}
void display(){cout<
CPoint operator ++ (void);
};
CPoint CPoint::operator ++ (void)
{
++x;
++y;
return *this;
}
int main(void)
{
CPoint c(1,1);
c++;
c.display();
return 0;
}
/*
* 成员函数重载运算符后缀"++"
* Lzy 2011-8-8
*/
#include
using namespace std;
class CPoint
{
private:
int x, y;
public:
CPoint(int _x=0, int _y=0):x(_x),y(_y){}
void display(){cout<
CPoint operator ++ (int);
};
CPoint CPoint::operator ++ (int)
{
return CPoint(x++, y++);
}
int main(void)
{
CPoint a,c(1,1);
a=c++;
a.display();
c.display();
return 0;
}
/*
* 友元函数重载运算符"++"
* Lzy 2011-8-8
*/
#include
using namespace std;
class CPoint
{
private:
int x, y;
public:
CPoint(int _x=0, int _y=0):x(_x),y(_y){}
void display(){cout<
friend CPoint operator ++ (CPoint &c); //前缀
friend CPoint operator ++ (CPoint &c, int); //后缀
};
CPoint operator ++ (CPoint &c)
{
return CPoint(++c.x, ++c.y);
}
CPoint operator ++ (CPoint &c,int)
{
return CPoint(c.x++, c.y++);
}
int main(void)
{
CPoint a,c(1,1);
a=c++;
a.display();
c.display();
a=++c;
a.display();
c.display();
return 0;
}
/*
* 重载运算符"="
* Lzy 2011-8-8
*/
#include
using namespace std;
class CPoint
{
private:
int x, y;
public:
CPoint(int _x=0, int _y=0):x(_x),y(_y){}
void display(){cout<
CPoint operator = (CPoint c);
};
CPoint CPoint::operator = (CPoint c)
{
x= c.x;
y= c.y;
return *this;
}
int main(void)
{
CPoint a,c(1,1);
a=c;
a.display();
return 0;
}
/*
* 下标运算符[]的重载
* Lzy 2011-8-8
*/
#include
using namespace std;
class CStrArray
{
private:
char *ptr;
int len;
public:
CStrArray(int i):len(i){ptr = new char[len];}
~CStrArray(){delete[] ptr;}
int getlen(){return len;}
char & operator[](int);
};
char & CStrArray::operator [] (int i)
{
static char ch = '\0';
if(i<=len && i>=0)
return *(ptr+i);
else
{
cout<<"数组下标越界"<
return ch;
}
}
int main(void)
{
CStrArray str(5);
cout<<"输入个字符: ";
for(int i=0; i<5; i++)
cin>>str[i];
for(int i=0; i<5; i++)
cout<
return 0;
}
/*
* 重载运算符"<<" 和">>"
* Lzy 2011-8-8
*/
#include
using namespace std;
class CPoint
{
private:
int x, y;
public:
CPoint(int _x=0, int _y=0):x(_x),y(_y){}
friend ostream & operator <<(ostream &, const CPoint &);
friend istream & operator >>(istream &, CPoint &);
};
ostream & operator <<(ostream &output, const CPoint &c)
{
output<
return output;
}
istream & operator >>(istream &input, CPoint &c)
{
// input.ignore(); //删除输入流中指定数目的字符,默认个数为
input>>c.x;
input>>c.y;
return input;
}
int main(void)
{
CPoint c;
cin>>c;
cout<
return 0;
}
/*
* 转换函数实现类型强制转换
* Lzy 2011-8-8
*/
#include
using namespace std;
class CPoint
{
private:
float x;
public:
CPoint(float _x=0):x(_x){}
operator int(){return (int)x;}
};
int main(void)
{
CPoint c(12.5);
int y = (int)c;
cout<
return 0;
}