?? 16-6.txt
字號:
/* 范例:16-6 */
#include <iostream.h>
class machine
{
public:
machine(string str):name(str){} /* machine類的構造函數 */
bool operator==(machine&); /* 聲明operator==,this是隱含的第一個參
數 */
private:
string name;
};
void main()
{
machine A_robot("Peter"); /* 構造一個machine類的對象A_robot */
machine B_robot("Peter_Tang"); /* 構造一個machine類的對象B_robot */
machine C_robot("Peter"); /* 構造一個machine類的對象C_robot */
if (A_robot==B_robot) /* 調用operator== */
cout <<"機器人A與機器人B的名字相同" <<endl;
else
cout <<"機器人A與機器人B的名字不相同" <<endl;
if (A_robot==C_robot) /* 調用operator== */
cout <<"機器人A與機器人C的名字相同" <<endl;
else
cout <<"機器人A與機器人C的名字不相同" <<endl;
getchar();
}
bool machine::operator==(machine& roblt)
{
if(this->name == roblt.name)
return true;
else
return false;
}
程序執行結果:
機器人A與機器人B的名字不相同
機器人A與機器人C的名字相同
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -