1 面向对象(C++)程序设计 (上机考试) 样题1. 下列Shape 类是一个表示形状的抽象类,Area()为求图形面积的函数,Total()则是一个通用的用以求不同形状的图形面积总和函数。请从Shape 类派生三角形类(triangle) 、 矩形类(rectangle),并给出具体的求面积函数。编写程序验证求面积函数的正确性。Shape、 total 的定义如下所示。 Class shape{ Pubilc: Virtual float area()=0 }; float total (shape *s[ ], int n) { float sum=0.0; for(int i=0; iarea (); return sum; } 解答: #include class shape{ public: virtual float area()=0; }; float total(shape *s[], int n) { float sum=0; for(int i=0; i area(); return sum; } class triangle : public shape{ protected: float H, W; public: triangle(float h, float w) { H=h; W=w;} float area() { return H*W*0.5;} }; class rectangle : public triangle{ public: rectangle(float h, float w) : triangle(h, w) {} float area() { return H*W;} }; void main() { shape *s[4]; s[0] = new triangle( 3.0, 4.0 ); s[1] = new rectangle( 2.0, 4.0 ); s[2] = new triangle( 5.0, 8.0 ); s[3] = new rectangle( 6.0, 8.0 ); float sum = total(s,4); cout << "The total area is:" << sum << endl; } 样题2. 以面向对象的概念设计一个类,此类包括3个私有数据,unlead(无铅汽油), lead有铅汽油, total (当天总收入)。其中,无铅汽油价格是¥17/升 ,有铅汽油价格是¥16/升 ,请以构 造 函数的方 式 建 立 此值 ,并编写程序,该 程序能 够 根 据加 油量 ,自 动 计算 出油站 当天的总收入。 解答: #include class income { private: float unlead, lead, total; public: income(float ul, float l){unlead=ul; lead=l; total=0.0;} float calculate(float, float); }; float income :: calculate(float unleadcontent, float leadcontent) { total = unlead*unleadcontent + lead*leadcontent; return total; } void main() { float unleadcontent, leadcontent,...