1 #include "stdafx.h" 2 #include <iostream> 3 #include <math.h> 4 using namespace std; 5 class Room 6 { 7 public: 8 Room() 9 { 10 } 11 ~Room() 12 { 13 } 14 void setHeight(double ht) 15 { 16 value = ht; 17 } 18 double getHeight() 19 { 20 return (value); 21 } 22 bool operator > (const Room& rh)//比较 23 { 24 if(value > rh.value) 25 return true; 26 else 27 return false; 28 } 29 Room operator+(Room& rh) 30 { 31 Room lh; 32 lh.value = value + rh.value; 33 return lh; 34 } 35 Room operator-(Room& rh) 36 { 37 Room lh; 38 lh.value = fabs(value - rh.value); 39 return lh; 40 } 41 double value; 42 }; 43 44 int _tmain(int argc, _TCHAR* argv[]) 45 { 46 Room room1,room2,room,room3; 47 room1.setHeight(4.6); 48 room2.setHeight(4.8); 49 if(room1 > room2) 50 cout<<"房间1比房间2高"<<endl; 51 else 52 cout<<"房间1比房间2低"<<endl; 53 room= room1+room2; 54 cout<<"房间高度和为 "<<room.value<<endl; 55 room3 = room1-room2; 56 cout<<"房间高度差为 "<<room3.value<<endl; 57 58 return 0; 59 }
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)