请教C++类型指针转换的问题
实际工作中遇到的问题,有一个计算,因为输入参数不同会产生不同的对象,比如三角形、四边形、五边形,于是设计了3个类
class sanjiaoxing
{
public:
virtual bool input(vector<double> in);
virtual double area();
protected:
point p1, p2, p3;
}
class sibianxing:public sanjiaoxing
{
public:
virtual bool input(vector<double> in);
virtual double area();
protected:
point p4;
}
class wubianxing:public sibianxing
{
public:
virtual bool input(vector<double> in);
virtual double area();
protected:
point p5;
}
由于用户输入的不同,将生成不同的形状,为了计算它们的面积我设定了一个指向对象的指针*ptr,并用ptr->area()来获得面积,但是问题在于ptr在初始化时应该怎么处理呢?
我的思路是先把*ptr定义为wubianxing,但是dynamic_cast不能改变一个指针的类型,只能把一个父类指针par的地址传递给另一个子类指针chi,而chi=par这个表达式编译通不过。
如果针对不同的输入、不同的形状分别写代码,那这个代码量就太大了,现在还只有3层继承关系,如果是30层,非得折磨死我不可。
哪位老鸟能给俺点指点?先谢过了
[解决办法]
class 图形
{
virtual double area( void ) = 0;
}
class 三角形 : public 图形
{
public:
三角形( p1, p2, p3 );//构造函数
virtual double area( void ) { };
};
class 四边形 : public 图形
{
public:
四边形( p1, p2, p3, p4 );//构造函数
virtual double area( void ) { };
};
class 五边形 : public 图形
{
public:
五边形( p1, p2, p3, p4, p5 );//构造函数
virtual double area( void ) { };
};
图形 * A = NULL;
if( 输入为三角形 )
A = new 三角形( p1, p2, p3 )
if( 输入为四边形 )
A = new 四边形( p1, p2, p3, p4 )
if( 输入为五边形 )
A = new 五边形( p1, p2, p3, p4, p5 )
A->area();
[解决办法]
如果纯粹研究有继承体系的指针类型转换,不用考虑工厂模式,那样会使得事情变得更加复杂。
不过楼主的问题的确比较适合用工厂模式来解决,可以参考下面的代码:
#include <iostream>#include <vector>#include <cmath>using namespace std;// 定义一个“点”类class Point{private: double x; double y;public: Point() { x = 0.0; y = 0.0; } Point(double x, double y) { this->x = x; this->y = y; } void set_X(double x) { this->x = x; } double get_X() { return x; } void set_Y(double y) { this->y = y; } double get_Y() { return y; }};// 定义几何图形类class Geometry{protected: vector<Point> points;public: virtual double calculateArea() = 0; virtual ~Geometry(){};};// 定义具体的几何图形类:三角形class Triangle : public Geometry{public: Triangle() { points.push_back(Point(0.0, 0.0)); points.push_back(Point(0.0, 0.0)); points.push_back(Point(0.0, 0.0)); } Triangle(const Point& a, const Point& b, const Point& c) { points.push_back(a); points.push_back(b); points.push_back(c); } double calculateArea() { double line1 = sqrt(pow((points[0].get_X() - points[1].get_X()), 2.0) + pow((points[0].get_Y() - points[1].get_Y()), 2.0)); double line2 = sqrt(pow((points[1].get_X() - points[2].get_X()), 2.0) + pow((points[1].get_Y() - points[2].get_Y()), 2.0)); double line3 = sqrt(pow((points[2].get_X() - points[0].get_X()), 2.0) + pow((points[2].get_Y() - points[0].get_Y()), 2.0)); double halfPerimeter = (line1 + line2 + line3) / 2.0; return sqrt(halfPerimeter * (halfPerimeter - line1) * (halfPerimeter - line2) * (halfPerimeter - line3)); }};// 定义具体的几何图形类:四边形class Quadrangle : public Geometry{public: Quadrangle() { points.push_back(Point(0.0, 0.0)); points.push_back(Point(0.0, 0.0)); points.push_back(Point(0.0, 0.0)); points.push_back(Point(0.0, 0.0)); } Quadrangle(const Point& a, const Point& b, const Point& c, const Point& d) { points.push_back(a); points.push_back(b); points.push_back(c); points.push_back(d); } double calculateArea() { double area = 0; double line1 = sqrt(pow((points[0].get_X() - points[1].get_X()), 2.0) + pow((points[0].get_Y() - points[1].get_Y()), 2.0)); double line2 = sqrt(pow((points[1].get_X() - points[2].get_X()), 2.0) + pow((points[1].get_Y() - points[2].get_Y()), 2.0)); double line3 = sqrt(pow((points[2].get_X() - points[3].get_X()), 2.0) + pow((points[2].get_Y() - points[3].get_Y()), 2.0)); double line4 = sqrt(pow((points[3].get_X() - points[0].get_X()), 2.0) + pow((points[3].get_Y() - points[0].get_Y()), 2.0)); double diagonal = sqrt(pow((points[0].get_X() - points[2].get_X()), 2.0) + pow((points[0].get_Y() - points[2].get_Y()), 2.0)); double halfPerimeter = (line1 + line2 + diagonal) / 2.0; area += sqrt(halfPerimeter * (halfPerimeter - line1) * (halfPerimeter - line2) * (halfPerimeter - diagonal)); halfPerimeter = (line3 + line4 + diagonal) / 2.0; area += sqrt(halfPerimeter * (halfPerimeter - line3) * (halfPerimeter - line4) * (halfPerimeter - diagonal)); return area; }};// 定义抽象工厂类class AbstractGeometryFactory{protected: Geometry* geometry;public: virtual Geometry* createGeometry(const vector<Point>& points) = 0; virtual ~AbstractGeometryFactory(){}};// 定义具体的工厂类:三角形工厂,用于创建三角形class TriangleFactory : public AbstractGeometryFactory{public: TriangleFactory() { geometry = NULL; } Geometry* createGeometry(const vector<Point>& points) { if(points.size() != 3) { cout << "It's not a triangle" << endl; } geometry = new Triangle(points[0], points[1], points[2]); return geometry; } ~TriangleFactory() { if(geometry != NULL) { delete geometry; geometry = NULL; } }};// 定义具体的工厂类:四边形工厂,用于创建四边形class QuadrangleFactory : public AbstractGeometryFactory{public: Geometry* createGeometry(const vector<Point>& points) { if(points.size() != 4) { cout << "It's not a Quadrangle" << endl; } geometry = new Quadrangle(points[0], points[1], points[2], points[3]); return geometry; } ~QuadrangleFactory() { if(geometry != NULL) { delete geometry; geometry = NULL; } }};// 程序入口:用于测试上面的代码int main(int argc, char** argv){ AbstractGeometryFactory* agf1; AbstractGeometryFactory* agf2; vector<Point> trianglePoints; vector<Point> quadranglePoints; Point a(0.0, 0.0); Point b(6.0, 0.0); Point c(6.0, 6.0); Point d(0.0, 6.0); trianglePoints.push_back(a); trianglePoints.push_back(b); trianglePoints.push_back(c); quadranglePoints.push_back(a); quadranglePoints.push_back(b); quadranglePoints.push_back(c); quadranglePoints.push_back(d); agf1 = new TriangleFactory; agf2 = new QuadrangleFactory; double triangleArea = (agf1->createGeometry(trianglePoints))->calculateArea(); cout << "The area of the triangle is:\t" << triangleArea << endl; double quadrangleArea = (agf2->createGeometry(quadranglePoints))->calculateArea(); cout << "The area of the quadrangle is:\t" << quadrangleArea << endl; delete agf1; delete agf2; return 0;}