繁体   English   中英

纯虚函数的不同参数 c++

[英]Different arguments to pure virtual functions c++

我正在做一个关于纯虚函数的作业。 我有两个不同的类,我希望使用纯虚函数。 虚函数用于计算面积,每个类即(正方形、三角形)使用不同的参数来计算面积。 我怎样才能让两个 getArea 函数与纯虚函数一起工作?

#include <iostream>
#include <cmath>

using namespace std;

class Point{
    public:
    double x, y;
    Point(double a = 0, double b = 0){x = a; y = b;}
    Point operator + (Point const &obj){
        Point p;
        p.x = x + obj.x;
        p.y = y + obj.y;
        return p;
    }
     Point operator - (Point const &obj){
        Point p;
        p.x = x - obj.x;
        p.y = y - obj.y;
        return p;
    }
    friend ostream& operator<<(ostream& os, const Point& pt);

};

class Shape{
    public:
    virtual double getArea(Point a, Point b, Point c, Point d) = 0 ; // this is where i have a problem
    // need this to be virtual void getArea() = 0; so i can use it for every other class but not sure how
};

class Square: public Shape{
    public:
 // find area from four points

    double length(Point a, Point b){
        double hDis = pow((b.x - a.x),2);
        double vDis = pow((b.y - a.y),2);
        return sqrt(hDis + vDis);
        }
    double area_triangle(Point a, Point b, Point c){
        double A = length(a, b);
        double B = length (b, c);
        double C = length(a, c);
        double S = (length(a, b) + length (b, c) + length(a, c))/2;
        double area = sqrt((S*(S-A)*(S-B)*(S-C)));
        return area;
        }

    double getArea(Point a, Point b, Point c, Point d){ // have to calculate area with the point coordinates
        double area_tri1 = area_triangle(a, b, c);
        double area_tri2 = area_triangle(a, d, c);
        double total_area = area_tri1 + area_tri2;
        return total_area;
    }
};


class Triangle: public Shape{
    public:
double length(Point a, Point b){
        double hDis = pow((b.x - a.x),2);
        double vDis = pow((b.y - a.y),2);
        return sqrt(hDis + vDis);
        }
    double getArea(Point a, Point b, Point c){
        double A = length(a, b);
        double B = length (b, c);
        double C = length(a, c);
        double S = (length(a, b) + length (b, c) + length(a, c))/2;
        double air = sqrt((S*(S-A)*(S-B)*(S-C)));
        return air;
        }
};

ostream& operator<<(ostream& os, const Point& pt)
{
    os << pt.x << ", " << pt.y <<endl;
    return os;
}


int main(){

    Point p1(5,-5), p2(-10,7), p3(4, 23), p4(-6, 12);

    Square s;
    cout << s.getArea(p1, p2, p3, p4);


    //! Triangle t;
    //! cout << t.getArea(p1, p2,p3);

    // this gives me an error because the abstract function want
    // (Point a, Point b, Point c, Point d)
    // how do i make this work?
}

你设计的课程有点奇怪。

它们应该具有定义其范围的数据成员(“成员变量”)(该特定类型所需的Point数量)。

那么就不需要将任何参数传递给getArea() :每个类中该函数的实现都将使用成员变量!

然后你就不会再有问题了,因为所有的getArea()函数都将有相同数量的参数:无。

如果你把东西从函数参数中移到成员变量中,这样你的类实际上包含一些state ,你应该会发现你的设计的其余部分围绕着这个变化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM