网站建设网站定制开发,wordpress文章页设置全屏,越秀区建网站的公司,重庆seo网站收录优化一、思维导图 二、作业#xff1a;实现关系运算符的重载
关系运算符重载 概念#xff1a; 种类#xff1a;、、 、 、 、!表达式#xff1a;L#R (L表示左操作数#xff0c;R表示有操作数#xff0c;#表示运算符)左操作数#xff1a;既可以是左值也可以…一、思维导图 二、作业实现关系运算符的重载
关系运算符重载 概念 种类、、 、 、 、!表达式L#R (L表示左操作数R表示有操作数#表示运算符)左操作数既可以是左值也可以是右值右操作数既可以是左值也可以是右值结果bool类型的数据右值定义格式 成员函数版const bool operator#(const 类名 R)const全局函数版const bool operator#(const 类名 L, const 类名 R) 关系运算符种类、、 、 、 、! 关系运算符重载的实现方法相同 2.1
① 成员函数重载
#include iostream
using namespace std;class Person
{
private:int a;int b;
public://无参构造函数Person() {}//有参构造函数Person(int a,int b):a(a),b(b){}//成员函数实现号运算符重载bool operator(const Person R) const{if(aR.a bR.b){return true;}else{return false;}}
};int main()
{Person s1(20,20);Person s2(2,1);if(s1.operator(s2)){cout s1s2 endl;}return 0;
}② 全局函数重载
#include iostream
using namespace std;class Person
{//将全局函数设置为友元friend bool operator(const Person R,const Person L);
private:int a;int b;
public://无参构造函数Person() {}//有参构造函数Person(int a,int b):a(a),b(b){}
};bool operator(const Person R,const Person L)
{//成员函数实现号运算符重载if(R.aL.a R.bL.b){return true;}else{return false;}
}
int main()
{Person s1(20,20);Person s2(2,1);if(operator(s1,s2)){cout s1s2 endl;}return 0;
}2.2 ① 成员函数重载
类内函数 //成员函数实现号运算符重载bool operator(const Person R) const{if(aR.a bR.b){return true;}else{return false;}}
main函数内的调用函数 if(s1.operator(s2)){cout s1s2 endl;}② 全局函数重载
#include iostream
using namespace std;class Person
{//将全局函数设置为友元friend bool operator(const Person R,const Person L);
private:int a;int b;
public://无参构造函数Person() {}//有参构造函数Person(int a,int b):a(a),b(b){}
};bool operator(const Person R,const Person L)
{//成员函数实现号运算符重载if(R.aL.a R.bL.b){return true;}else{return false;}
}
int main()
{Person s1(20,20);Person s2(2,1);if(operator(s1,s2)){cout s1s2 endl;}return 0;
}