1 Star 0 Fork 1

llongger/Code_C++

forked from GodOuO/Code_C++ 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
36.Operator=_relo.cpp 971 Bytes
一键复制 编辑 原始数据 按行查看 历史
GodOuO 提交于 2023-03-15 04:07 . Train
#include<iostream>
#include<string>
using namespace std;
//关系运算符重载
class Person
{
private:
string m_Name;
int m_Age;
public:
Person(string name,int age);
bool operator==(Person &p); //成员重载 ==
bool operator!=(Person &p); //成员重载 !=
};
Person::Person(string name,int age)
{
m_Name = name;
m_Age = age;
}
bool Person::operator==(Person &p){
if(this->m_Age == p.m_Age && this->m_Name == p.m_Name)
return 1;
else
return 0;
}
bool Person::operator!=(Person &p){
if(this->m_Age == p.m_Age && this->m_Name == p.m_Name)
return 0;
else
return 1;
}
void test01(){
Person p1("Tom",18);
Person p2("Tom",18);
if(p1 == p2)
cout<<"P1 is same as P2!"<<endl;
else
cout<<"P1 and P2 are different!"<<endl;
if (p1!=p2)
cout<<"P1 and P2 are different!"<<endl;
else
cout<<"P1 is same as P2!"<<endl;
}
int main(){
test01();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/llongger/codeCpp.git
[email protected]:llongger/codeCpp.git
llongger
codeCpp
Code_C++
master

搜索帮助