【Linux基础】运算符重载为类的成员函数

双目运算符重载
双目运算符,如果重载为类的成员函数,其参数为一个,即比运算对象少一个。
例8.2 复数的乘法运算,在上例的基础上添加乘法运算符重载函数。复数类乘法运算的定义如下:
(a+bi)*(x+yi)= a*x-b*y + (a*y + b*x)i
例2 复数乘法运算源程序
#include "iostream.h"
class CComplex
{
private:
double real;
double imag;
public:
CComplex(double r=0, double i=0);
void Print();
CComplex operator +(CComplex c);
CComplex operator -(CComplex c);
CComplex operator *(CComplex c);
};
CComplex::CComplex (double r, double i)
{
real = r;
imag = i;
}
void CComplex:rint()
{
cout << "(" << real << "," << imag << ")" << endl;
}
CComplex CComplex:perator +(CComplex c)
{
CComplex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
CComplex CComplex:perator -(CComplex c)
{
CComplex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
CComplex CComplex:perator *(CComplex c)
{
CComplex temp;
temp.real = real * c.real - imag * c.imag;
temp.imag = real * c.imag + imag * c.real;
return temp;
}
void main(void)
{
CComplex a(1, 2), b(3.0, 4.0), c, d, e, f;
c = a+b;
d = a-b;
e = a*b;
f = a+1;
cout << "c = ";
c.Print();
cout << "d = ";
d.Print();
cout << "e = ";
e.Print();
cout << "f = ";
f.Print();
}
程序运行结果为:
c = (4, 6)
d = (-2, -2)
e = (-5,10)
f = (2, 2)
单目运算符重载
单目运算符,如果重载为类的成员函数,不需要参数。
为区分前置和后置运算符,C++规定:
对于前置单目运算符,重载函数没有参数
对于后置单目运算符,重载函数有一个整型参数,这个整型参数没有其他用途,只是用于区分前置运算与后置运算。
例3 定义一个CInt类,类中只有一个数据成员i,两个运算符“++”的重载函数,一个没有参数,实现的是前置运算符重载,另一个有一个整型参数,实现后置运算符重载。
#include "iostream.h"
class CInt
{
private:
int i;
public:
CInt(int a=0);
void Print();
CInt operator ++();
CInt operator ++(int);
};
CInt::CInt (int a)
{
i = a;
}
void CInt:rint()
{
cout << "i=" << i << endl;
}
CInt CInt:perator ++()
{
CInt temp;
temp.i = ++i;
return temp;
}
CInt CInt:perator ++(int)
{
CInt temp;
temp.i = i++;
return temp;
}
void main(void)
{
CInt a(5), b(5), c, d;
c = a++;
d = ++b;
cout << "a: ";
a.Print();
cout << "b: ";
b.Print();
cout << "c: ";
c.Print();
cout << "d: ";
d.Print();
}
程序运行结果为:
a: i=6
b: i=6
c: i=5
d: i=6
赋值运算符重载
如果类中只包含简单数据类型的数据成员,则使用C++提供的赋值运算符“=”就可以实现将一个对象赋给另一个对象。如前面复数类的对象,就可以将一个对象直接赋给另一个对象。但如果类的数据成员比较复杂(如含有指针),这样直接赋值就会产生问题,我们必须重载赋值运算符“=”才能正确使用“=”。
例 4 类A只有一个数据成员str,是一个字符指针,在构造函数中为str申请存储空间并赋值,在析构函数中释放内存。
#include "iostream.h"
#include "string.h"
class A
{
private:
char *str;
public:
A(char *s="no data");
~A();
void print();
};
A::A (char *s)
{
int len = strlen(s);
str = new char[len+1];
strcpy(str,s);
}
A::~A ()
{
if(str)
delete []str;
}
void A::print()
{
cout << str << endl;
}
void main(void)
该语句只是将p所指向的对象数据成员str赋给对象a1的数据成员str,即两个对象的str指向了同一个单元
{
A *p = new A("AAAA");
A a1;
a1=*p;
a1.print();
delete p;
调用析构函数,同时将str所指向的单元释放了,再执行a1.print()时,就会出现错误。
a1.print();
}
例4 带有重载赋值运算符的A类
#include "iostream.h"
#include "string.h"
class A
{
private:
char *str;
public:
A(char *s="no data");
~A();
A &operator =(A &a);
void print();
};
A::A (char *s)
{
int len = strlen(s);
str = new char[len+1];
strcpy(str,s);
}
A::~A ()
{
if(str)
delete []str;
}
A &A:perator =(A &a)
{
int len = strlen(a.str);
if(str)
delete []str; //先释放,再根据实际需要重新申请
str = new char[len+1];
strcpy(str, a.str);
return *this;
}
void A::print()
{
cout << str << endl;
}
void main(void)
{
A *p = new A("AAAA");
A a1;
a1=*p;
a1.print();
delete p;
a1.print();
}
程序运行结果为:
AAAA
AAAA
本文转载于唯C教育,【Linux基础】运算符重载为类的成员函数
http://www.weicedu.com/forum.php?mod=viewthread&tid=77&fromuid=4
(出处: http://www.weicedu.com/)
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值