Last active
September 28, 2018 00:48
-
-
Save qiaoxu123/47890b5bcc2169eba19610c1202cf428 to your computer and use it in GitHub Desktop.
测试C++ this指针的用法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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