14.5
View Code
1 /* 2 author:shajin 3 date:2013-4-6 4 */ 5 #include6 #include 7 using namespace std; 8 class Shape 9 { 10 public: 11 double area(); 12 double girth(); 13 void show(); 14 }; 15 double Shape::area() 16 { 17 return 0; 18 } 19 20 double Shape::girth() 21 { 22 return 0.0; 23 } 24 25 void Shape::show() 26 { 27 cout<<"Shape Object:"< a=a; 78 this->b=b; 79 this->c=c; 80 } 81 double Triangle::area() 82 { 83 double s=(a+b+c)/2; 84 return sqrt(s*(s-a)*(s-b)*(s-c)); 85 } 86 double Triangle::girth() 87 { 88 return a+b+c; 89 } 90 void Triangle::show() 91 { 92 cout<<"Triangle:"< << <
14.6
View Code
1 // 文件point.h: 类Point的定义 2 #if !defined __POINT__H__ 3 #define __POINT__H__ 4 5 #include6 7 class Point 8 { 9 friend ostream &operator << (ostream &, const Point &);10 public:11 // 重载的构造函数12 Point(double = 0, double = 0);13 Point(const Point &p); // 复制构造函数14 15 // 析构函数16 ~Point();17 18 // 重载的定值函数19 void setPoint(double a, double b);20 void setPoint(Point &p);21 22 // 取值函数23 double getX()const24 {25 return x;26 } double getY()const27 {28 return y;29 }30 31 private:32 double x, y;33 };34 35 #endif
View Code
1 // 文件point.cpp: 类Point的实现 2 #include "point.h" 3 4 Point::Point(double a, double b) 5 { 6 x = a; 7 y = b; 8 } 9 10 Point::Point(const Point &p)11 {12 x = p.getX();13 y = p.getY();14 }15 16 Point::~Point(){}17 18 void Point::setPoint(double a, double b)19 {20 x = a;21 y = b;22 }23 24 void Point::setPoint(Point &p)25 {26 x = p.getX();27 y = p.getY();28 }29 30 ostream &operator << (ostream &os, const Point &point)31 {32 os << "(" << point.x << ", " << point.y << ")";33 return os;34 }
View Code
1 // 文件line.h: 类Line的定义 2 #if !defined __LINE__H__ 3 #define __LINE__H__ 4 5 #include6 #include "point.h" 7 8 class Line { 9 friend ostream & operator<< (ostream &, const Line &);10 public:11 // 重载的构造函数12 Line(double startX = 0, double startY = 0, double endX = 0, double endY = 0);13 Line(Point start, Point end);14 Line(Line &line); //复制构造函数15 16 // 析构函数17 ~Line();18 19 // 重载的定值函数20 void setLine(double startX = 0, double startY = 0, double endX = 0, double endY = 0);21 void setLine(Point , Point );22 23 double getLength() const; // 计算线段的长度24 25 // 取值函数26 Point getStartPoint() { return startPoint;}27 Point getEndPoint() { return endPoint;}28 29 private:30 Point startPoint, endPoint;31 };32 33 #endif