Skip to content

Instantly share code, notes, and snippets.

@qiaoxu123
Last active September 28, 2018 00:48
Show Gist options
  • Save qiaoxu123/47890b5bcc2169eba19610c1202cf428 to your computer and use it in GitHub Desktop.
Save qiaoxu123/47890b5bcc2169eba19610c1202cf428 to your computer and use it in GitHub Desktop.
测试C++ this指针的用法
#include <iostream>
using namespace std;
class Box {
private:
double length;
double breadth;
double height;
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0) { //构造函数
cout << "Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume() { //体积
return length * breadth * height;
}
int compare(Box box) { //体积对比
return this->Volume() > box.Volume(); //关键部分
}
};
int main(void) {
Box Box1(3.3, 1.2, 1.5);
Box Box2(8.5, 6.0, 2.0);
if (Box1.compare(Box2)) {
cout << "Box2 is smaller than Box1" << endl;
}
else {
cout << "Box2 is equal to or larger than Box1" << endl;
}
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment