使用面向过程的方法:
#include <iostream>
using namespace std;
class Cube{
private:
int m_a;
int m_b;
int m_c;
public:
//设置立方体的长宽高
void setABC(int a, int b, int c){
m_a = a;
m_b = b;
m_c = c;
}
int getA() {
return m_a;
}
int getB() {
return m_b;
}
int getC() {
return m_c;
}
};
//判断两个立方体是否相等
bool judge(Cube& c1, Cube& c2)
{
if (c1.getA() == c2.getA() &&
c1.getB() == c2.getB() &&
c1.getC() == c2.getC()) {
return true;
}
else {
return false;
}
}
int main(void){
Cube c1;
c1.setABC(10, 20, 30);
Cube c2;
c2.setABC(10, 20, 30);
// 面向过程的判断方法
if (judge(c1, c2) == true) {
cout << "相同" << endl;
}
else {
cout << "不同" << endl;
}
}
使用面向对象的方法:
#include <iostream>
using namespace std;
class Cube{
private:
int m_a;
int m_b;
int m_c;
public:
//设置立方体的长宽高
void setABC(int a, int b, int c)
{
m_a = a;
m_b = b;
m_c = c;
}
int getA() {
return m_a;
}
int getB() {
return m_b;
}
int getC() {
return m_c;
}
//提供一个判断两个立方体是否相等的成员函数
bool judge(Cube& another_c){
if (m_a == another_c.m_a && // 同类之间无私处
m_b == another_c.getB() &&
m_c == another_c.getC()) {
return true;
} else {
return false;
}
}
};
int main(void){
Cube c1;
c1.setABC(10, 20, 30);
Cube c2;
c2.setABC(10, 20, 30);
//面向对象的判断方法
if (c1.judge(c2) == true) {
cout << "相同" << endl;
}
else {
cout << "不同" << endl;
}
}