4-9
设计并测试一个名为Rectangle 的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。
1 #include <iostream> 2 #include <string> 3 #include <stdio.h> 4 using namespace std; 5 6 class Rectangle{ 7 private: 8 int left,top,right,bottom; 9 public: 10 Rectangle(){} 11 Rectangle(int l, int t, int r, int b):left(l),top(t),right(r),bottom(b){ 12 } 13 int area(){ 14 return (right-left)*(top-bottom); 15 } 16 ~Rectangle(){} 17 }; 18 19 int main(){ 20 Rectangle test(1,4,3,2); 21 cout<<"面积为"<<test.area(); 22 return 0; 23 }

浙公网安备 33010602011771号