C++继承与多态练习--计算图形面积
程序员文章站
2022-07-04 23:12:05
1.目的: 2.代码如下: 3 . 测试截图 4.关于多态性 ......
1.目的:
/*设计一个计算图形面积的类库。 类库的顶层是一个抽象类,并且提供三个纯虚函数;显示数据成员、返回面积和返回体积。 Class Shape { virtual void showData()=0; virtual double reArea()=0; virtual double reVolume()=0; }; 第二层由Shape类派生TwoDimShape(二维图形)和ThreeShape(三维图形), 它们增加了有关的数据成员,但没有成员函数的实现。 第三层派生具体的图形类。TwoDimShape类派生Circle(圆)、Elipse(椭圆)、 Rectangle(矩形)和Triangle(三角形)等类。 ThreeShape类派生Ball(球体)、Cylinder(圆柱体)、 RectangularParallelepiped(长方体)等类。 在主函数测试中使用多态方式调用不同对象的求值函数。 */
2.代码如下:
/* *This file contains code for C++ 6th experiment *By LZH */ #include<iostream> #include<string> using namespace std; const double PI = acos(-1.0); // Definition of Shape class and related functions goes here class Shape { virtual void showData() = 0; virtual double reArea() = 0; virtual double reVolume() = 0; }; class TwoDimShape :virtual public Shape { protected: double x, y; public: virtual void showData() { return ; } virtual double reArea() { return 0; } virtual double reVolume() { return 0; } }; class ThreeShape :virtual public Shape { protected: double x, y, z; public: virtual void showData() { return; } virtual double reArea() { return 0; } virtual double reVolume() { return 0; } }; class Circle :virtual public TwoDimShape { public: Circle(double tp) { x = tp; } Circle(Circle &asp) { x = asp.x; } ~Circle() { } void showData() { cout << "This is a Circle:" << endl << "The radiation:" << x << endl << "The area:" << reArea() << endl; } double reArea() { return PI*x*x; } }; class Elipse :virtual public TwoDimShape { public: Elipse(double ta, double tb) { x = ta, y = tb; } Elipse(Elipse &asp) { x = asp.x, y = asp.y; } ~Elipse() { } void showData() { cout << "This is a Elipse:" << endl << "The long axis:" << x << endl << "The short axis:" << y << endl << "The area:" << reArea() << endl; } double reArea() { return PI*x*y; } }; class Rectangle :virtual public TwoDimShape { public: Rectangle(double ta, double tb) { x = ta, y = tb; } Rectangle(Rectangle &asp) { x = asp.x, y = asp.y; } ~Rectangle() { } void showData() { cout << "This is a Rectangle:" << endl << "The long axis:" << x << endl << "The short axis:" << y << endl << "The area:" << reArea() << endl; } double reArea() { return x*y; } }; class Triangle :virtual public TwoDimShape { public: Triangle(double ta, double tb) { x = ta, y = tb; } Triangle(Triangle &asp) { x = asp.x, y = asp.y; } ~Triangle() { } void showData() { cout << "This is a Triangle:" << endl << "The base length:" << x << endl << "The height :" << y << endl << "The area:" << reArea() << endl; } double reArea() { return x*y / 2.0; } }; class Ball :virtual public ThreeShape { public: Ball(double ta) { x = ta; } Ball(Ball &asp) { x = asp.x; } ~Ball() { } void showData() { cout << "This is a Ball:" << endl << "The radiation:" << x << endl << "The surface area:" << reArea() << endl; } double reArea() { return PI*pow(x, 3)*4.0 / 3.0; } double reVolume() { return PI*x*x; } }; class Cylinder :virtual public ThreeShape { public: /* V=PI*r*r*h S=2*PI*r+r*h */ Cylinder(double ta, double tb) { x = ta, y = tb; } Cylinder(Cylinder &asp) { x = asp.x, y = asp.y; } ~Cylinder() { } void showData() { cout << "This is a Cylinder:" << endl << "The radiation:" << x << endl << "The height:" << y << endl << "The surface area:" << reArea() << endl; } double reArea() { return 2 * PI*x + x*y; } double reVolume() { return PI*x*x*y; } }; //RectangularParallelepiped class cuboid :virtual public ThreeShape { public: cuboid(double ta, double tb, double tc) { x = ta, y = tb, z = tc; } cuboid(cuboid &asp) { x = asp.x, y = asp.y, z = asp.z; } void showData() { cout << "This is a cuboid:" << endl << "The length:" << x << endl << "The width:" << y << endl << "The height" << z << endl << "The surface area:" << reArea() << endl; } double reArea() { return 2 * (x*y + x*z + y*z); } double reVolume() { return x*y*z; } }; int main(void) { TwoDimShape a; ThreeShape b; TwoDimShape *p = &a; ThreeShape *w = &b; Circle t1(1.0); Elipse t2(1.0, 2.0); Rectangle t3(10.0,2.3); Triangle t4(4.0, 5.0); Ball t5(2.33333); Cylinder t6(4.5, 65.0); cuboid t7(132, 5,156); p = &t1; p->showData(); p = &t2; p->showData(); p = &t3; p->showData(); p = &t4; p->showData(); w = &t5; w->showData(); w = &t6; w->showData(); w = &t7; w->showData(); return 0; }
3 . 测试截图
4.关于多态性
在这个例子中我用了基类指针指向基类,这个不难理解,在类型兼容规则下, 指向基类的指针可以隐式的转换成派生类的指针。 这是最常见的关于多态的用法,利用该指针指向任意一个子类对象, 就可以调用相应的虚函数,指向的子类的不同,实现的方法也就不同。
上一篇: 设计模式总结