成员函数重载运算符(c++中如何正确使用成员函数或者友元函数重载运算符)

本文目录
- c++中如何正确使用成员函数或者友元函数重载运算符
- 用成员函数重载运算符,使其对整型的运算符 = + -适用于分数运算
- C++中关于用成员函数实现运算符重载的问题
- 比较用成员函数和用友元函数重载运算符的异同点
- 以成员函数的形式重载关系运算符,使用半径比较Circle对象大小
c++中如何正确使用成员函数或者友元函数重载运算符
只要有一个操作数是自定义类型就可以重载。都是内置类型则不允许重载,比如你重载了算数+,编译器会乱套的。给你个例子: #include class A { public: A():a(0){} public: void display(){std::cout《
用成员函数重载运算符,使其对整型的运算符 = + -适用于分数运算
#include 《iostream》
using namespace std;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
class Rational
{
Rational make(int ta,int tb)
{
Rational x;
if(ta%tb==0)
{
x.a=ta/tb;
x.b=1;
}
else
{
int temp=ta》0?ta:-ta;
int t=gcd(temp,tb);
x.a=ta/t;
x.b=tb/t;
}
return x;
}
public:
int a,b;
Rational(int x,int y)
{
Rational t=make(x,y);
a=t.a;
b=t.b;
}
Rational(int x)
{
a=x;
b=1;
}
Rational()
{
a=0;
b=1;
}
bool operator==(Rational y)
{
return (a*y.b==b*y.a);
}
bool operator!=(Rational y)
{
return (a*y.b!=b*y.a);
}
bool operator《(Rational y)
{
return (a*y.b《b*y.a);
}
bool operator《=(Rational y)
{
return (a*y.b《=b*y.a);
}
bool operator》(Rational y)
{
return (a*y.b》b*y.a);
}
bool operator》=(Rational y)
{
return (a*y.b》=b*y.a);
}
Rational operator+(Rational temp)
{
return make(a*temp.b+b*temp.a,b*temp.b);
}
Rational operator-(Rational temp)
{
return make(a*temp.b-b*temp.a,b*temp.b);
}
Rational operator*(Rational temp)
{
return make(a*temp.a,b*temp.b);
}
Rational operator/(Rational temp)
{
return make(a*temp.b,b*temp.a);
}
friend ostream& operator《《(ostream&os,const Rational&x);
friend istream& operator》》(istream&is,Rational&x);
};
ostream& operator《《(ostream&os,const Rational&x)
{
if(x.b==1)
os《《x.a;
else
os《《x.a《《“/“《《x.b;
return os;
}
istream& operator》》(istream&is,Rational&x)
{
is》》x.a》》x.b;
if(x.b《0)
{
x.a=-x.a;
x.b=-x.b;
}
return is;
}
int main()
{
Rational p,q;
while(cin》》p》》q)
{
cout《《p+q《《“ “《《p-q《《“ “《《p*q《《“ “《《p/q《《“ “;
if(p《q)
cout《《“true“《《endl;
else
cout《《“false“《《endl;
}
return 0;
}
C++中关于用成员函数实现运算符重载的问题
对于同一个类的两个成员是不能互相访问私有数据的.
但也涉及到一个说法问题
假设这个类为
class Classtest{
private:
int value;
public:
//两个构造函数 :
Classtest(){value=0;}
Classtest(int j){value=j;}
//设置,和取值函数 :
void setvalue(int val){value=val;}
int getvalue(){return value;}
//重载加法操作符 :
Classtest operator +(Classtest&b){
return Classtest(this-》value+b.value);
}
};
A+B
可以获得结果是因为执行了A.operator+(B);
该函数是A的成员函数所以可以直接访问A的私有数据
由于在类内 所以B也可以访问自己的私有数据.
但不是A在访问B的私有数据.
注意这句话
int operator +(Classtest&b){return this-》value+b.value;}
对于B是通过b.value获得值,对于this是通过直接访问变量value来获得的.
如果在类外调用B.value;就会出错
因为类外B无法访问自己的私有数据,只能调用B的公有成员函数(该函数是类内的代码,所以他可以访问类的私有数据)
------------------------------
#include 《stdio.h》
#include 《stdlib.h》
class Classtest{
private:
int value;
public:
//两个构造函数 :
Classtest(){value=0;}
Classtest(int j){value=j;}
//设置,和取值函数 :
void setvalue(int val){value=val;}
int getvalue(){return value;}
//重载加法操作符 :
Classtest operator +(Classtest&b){
return Classtest(this-》value+b.value)//这个地方也可以使用b.getvalue();;
}
};
int main(void)
{
class Classtest j,b;
j.setvalue(2);
b.setvalue(4);
printf(“%d\n“,(j+b).getvalue());
;system(“pause“);
}
比较用成员函数和用友元函数重载运算符的异同点
重载双目运算符可以用友元或者成员函数,但是两者有区别
例如用成员函数重载了
+
之后(假设赋值运算符
=
已经被重载)
ob
=
ob
+
100是被支持的,而ob
=
100
+
ob就是不被允许的
这种情况下就要使用友元函数来对运算符重载以避免不必要的麻烦
以成员函数的形式重载关系运算符,使用半径比较Circle对象大小
#include《iostream》
class Circle
{
public:
Circle(float r):r(r)
{
}
bool operator《(const Circle &c)
{
return r《c.r;
}
bool operator》(const Circle &c)
{
return r》c.r;
}
bool operator==(const Circle &c)
{
return r==c.r;
}
bool operator》=(const Circle &c)
{
return !(*this《c);
}
bool operator《=(const Circle &c)
{
return !(*this》c);
}
bool operator!=(const Circle &c)
{
return !(*this==c);
}
private:
float r;
};
using namespace std;
int main()
{
float r1,r2;
cin》》r1》》r2;
Circle c1(r1),c2(r2);
cout《《boolalpha;
cout《《(c1《c2)《《endl;
cout《《(c1《=c2)《《endl;
cout《《(c1==c2)《《endl;
cout《《(c1!=c2)《《endl;
cout《《(c1》c2)《《endl;
cout《《(c1》=c2)《《endl;
return 0;
}

更多文章:
another time(another time和other time的区别)
2026年10月11日 05:00







